Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <stdlib.h>
#define PI 3.14159265358979323846
float fn(float x)
{
float integrand;
integrand = (1.0/(1.0+x*x));
return integrand;
}
int main()
{
char str[100000];
int i,N;
float a,b,sum=0,result=0,h;
float error;
printf("Enter the no of equally spaced points as a string =");
scanf("%s", str);
N=atoi(str);
printf("Enter the lower limit=");
scanf("%f",&a);
printf("Enter the upper limit=");
scanf("%f",&b);
h=(b-a)/(N-1);
for(i=1;i<=N;i++)
{
sum=sum+fn(a+i*h);
result=(fn(a)+fn(b)+2*sum)*h/2;
error = fabs((atan(b)-atan(a))-result);
//error = PI/2.0 - result;
printf("N=%d result=%f error=%f\n", i, result, error);
}
printf("final result =%f\n", result);
printf("cumulative error =%f\n", error);
}
Now, I want to write a unix script which calls the program for N = 2^{i} for integers i from 1, 2, 3, ..., 20 and use the Unix redirect operator > to record this data in a file.
I am searching over internet how to pass argument through shell and even how to modify my code so that it reads that passed parameter, still no good answer. Your suggestions are highly appreciated. It will help me learn the unix shell scripting as well.