How to write a C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table in C Programming Language ?
List of functions:
- checking initial values (checkVal)
- calculate x(calX)
- calculate value(s) of y(calY)
- output the table of function(output)
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- /*
- This program calculates a given function in range given by user, stores the data in arrays
- and displays the answer in a table.
- List of functions:
- checking initial values (checkVal)
- calculate x(calX)
- calculate value(s) of y(calY)
- output the table of function(output)
- */
- int checkVal(int N) //Number of steps (N) must be greater than zero
- {
- while(N<=0)
- {
- printf("N must be a positive integer!\n");
- printf("Please enter N\n");
- scanf("%i", &N);
- }
- return N;
- }
- void calcX(float A, float B, int N, float arrayX[])
- {
- int counter=0;
- float H=(B-A)/N;
- float X;
- while(counter<=N && counter<=15)
- {
- arrayX[counter]=A+(counter*H);
- counter++;
- }
- }
- void calcY(float arrayX[], float arrayY[], int N)
- {
- float euler=2.7182818;
- float X;
- int counter=0;
- while(counter<=N && counter<=15)
- {
- float X=arrayX[counter];
- float power1=X-(1/sin(X));
- if(sin(X)==0)
- {
- arrayY[counter] = -1;
- counter++;
- }
- else
- {
- float powerEuler=pow(euler, power1);
- float Y=pow(powerEuler, 0.25);
- arrayY[counter] = Y;
- counter++;
- }
- }
- }
- void output(int N, float arrayY[], float arrayX[])
- {
- int counter=0;
- printf("Step %-12s %10s\n", "VALUE OF X", "VALUE OF Y");
- while(counter<=N && counter<=15)
- {
- if(arrayY[counter]==-1)
- {
- printf("%4i %-12f %10s\n",counter, arrayX[counter], "irrational");
- counter++;
- }
- else
- {
- printf("%4i %-12f %-10f\n",counter, arrayX[counter], arrayY[counter]);
- counter++;
- }
- }
- }
- int main()
- {
- float arrayX[16];
- float arrayY[16];
- float A;
- float B;
- int N;
- int counter=0;
- printf("Please enter start point of X\n");
- scanf("%f", &A);
- printf("Please enter end point of X\n");
- scanf("%f", &B);
- printf("Please enter number of steps N\n");
- scanf("%i", &N);
- N=checkVal(N);
- calcX(A, B, N, arrayX);
- calcY(arrayX, arrayY, N);
- output(N, arrayY, arrayX);
- return 0;
- }