Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char* argv [])
{
FILE *input;
double valid1, valid_input, b, tmp, *x, *fx, *dx, area, total_area;
int rec, ns, i, curr_col, col;
input = fopen("px270prog3a.dat", "r");
if(argc != 2)
{
printf("Please enter one argument.\n");
exit(EXIT_FAILURE);
}
else
{
valid1=sscanf(argv[1], "%f", &b);
valid_input = (valid1 != 0) && (valid1 != EOF) && (b <= (M_PI / 2));
if(!valid_input)
{
printf("Invalid input, please enter a value between 0 and Pi/2.\n");
exit(EXIT_FAILURE);
}
if(input == (FILE*) NULL)
{
printf("Could not open input file\n");
}
ns = 0;
while(fscanf(input,"%f",&tmp) == 1) ns++;
ns = ns / 2;
x = (double*) malloc((ns+1)*sizeof(double));
fx = (double*) malloc((ns+1)*sizeof(double));
printf("Allocated memory for %d records\n",ns+1);
rewind(input);
i=0; rec = 0;
while(fscanf(input,"%lf",&tmp) == 1)
{
curr_col = (rec % 2);
if(curr_col==1)
{
fx[i] = (double)(tmp);
}
else if(curr_col==col)
{
x[i] = (double)(tmp);
i++;
}
rec++;
}
x[0] = 0;
dx[0] = 0;
fx[0] = 1;
i = 1;
area = 0;
total_area = 0;
while(i < (b * 20000 / M_PI))
{
dx[i] = x[i] * x[i-1];
i++;
}
while(i < (b * 20000 / M_PI))
{
area = fx[i-1] * dx[i];
total_area = total_area + area;
i++;
}
printf("Area under the curve is %f \n", total_area);
return(0);
}
}

I've been given an assignment to find the area under a cos^2(x) curve. The input file consists of 10000 values in each of 2 collumns, the first collumn is the value of x and the second collumn is the value of the function at that point.

I then have to find the area under the curve (between 0 and the point b which must be less than pi/2) using the following function:

(sum of) (f(xi) * ((xi) - (x(i-1)) between i=1 and i=b (the i's are supposed to be subscript).

Anyway, the code is how far I've got so far, it compiles but it gives the total area to be 0 unless b is 0.97 or higher, in which case it says "bus error".

Any advice you can offer will be greatly appreciated.