C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.

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: 
  1. checking initial values (checkVal)
  2. calculate x(calX)
  3. calculate value(s) of y(calY)
  4. output the table of function(output)

Solution:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. /*
  6. This program calculates a given function in range given by user, stores the data in arrays
  7. and displays the answer in a table.
  8.  
  9. List of functions:
  10.         checking initial values (checkVal)
  11.         calculate x(calX)
  12.         calculate value(s) of y(calY)
  13.         output the table of function(output)
  14. */
  15. int checkVal(int N) //Number of steps (N) must be greater than zero
  16. {
  17.         while(N<=0)
  18.         {
  19.                 printf("N must be a positive integer!\n");
  20.                 printf("Please enter N\n");
  21.                 scanf("%i", &N);
  22.         }
  23.         return N;
  24. }
  25. void calcX(float A, float B, int N, float arrayX[])
  26. {      
  27.         int counter=0;
  28.         float H=(B-A)/N;
  29.         float X;
  30.         while(counter<=&& counter<=15)
  31.         {
  32.                 arrayX[counter]=A+(counter*H);
  33.                 counter++;
  34.         }
  35. }
  36. void calcY(float arrayX[], float arrayY[], int N)
  37. {      
  38.         float euler=2.7182818;
  39.         float X;
  40.         int counter=0;
  41.         while(counter<=&& counter<=15)
  42.         {
  43.                 float X=arrayX[counter];
  44.                 float power1=X-(1/sin(X));
  45.                 if(sin(X)==0)
  46.                 {
  47.                         arrayY[counter] = -1;
  48.                         counter++;
  49.                 }
  50.                 else
  51.                 {
  52.                         float powerEuler=pow(euler, power1);
  53.                         float Y=pow(powerEuler, 0.25);
  54.                         arrayY[counter] = Y;
  55.                         counter++;
  56.                 }
  57.         }
  58. }
  59. void output(int N, float arrayY[], float arrayX[])
  60. {
  61.         int counter=0;
  62.         printf("Step    %-12s %10s\n", "VALUE OF X", "VALUE OF Y");
  63.         while(counter<=&& counter<=15)
  64.         {
  65.                 if(arrayY[counter]==-1)
  66.                 {
  67.                         printf("%4i    %-12f %10s\n",counter, arrayX[counter], "irrational");
  68.                         counter++;
  69.                 }
  70.                 else
  71.                 {
  72.                         printf("%4i    %-12f %-10f\n",counter, arrayX[counter], arrayY[counter]);
  73.                         counter++;
  74.                 }
  75.         }
  76. }
  77. int main()
  78. {
  79.         float arrayX[16];
  80.         float arrayY[16];
  81.         float A;
  82.         float B;
  83.         int N;
  84.         int counter=0;
  85.  
  86.         printf("Please enter start point of X\n");
  87.         scanf("%f", &A);
  88.         printf("Please enter end point of X\n");
  89.         scanf("%f", &B);
  90.         printf("Please enter number of steps N\n");
  91.         scanf("%i", &N);
  92.        
  93.         N=checkVal(N);
  94.         calcX(A, B, N, arrayX);
  95.         calcY(arrayX, arrayY, N);
  96.         output(N, arrayY, arrayX);
  97.  
  98.         return 0;
  99. }


Learn More :