1. ## Calculating an integral

i have to write a program to calculate the area under cos^2(x) but no matter what i do i keep getting 0

I've tried simplifying it down to this

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char* argv[])
{
FILE         *input;
double 	   b;
const char    inp_fn[]="px270prog3a.dat";
int            i;
double       x[10000], f[10000], integral;

input = fopen(inp_fn, "r");

b=1;

for(i=0; i <10000; i++)
{
fscanf(input, "%f %f", &x[i], &f[i]);

}

integral=0;

for(i=0; x[i]<=b; i++)
{
integral += f[i-1]*(x[i]-x[i-1]);

}

printf("Area = %.4lf \n", integral);

return(0);
}```
but it's still always 0.

Any help?

2. Take a look at your last for() loop... Is x[i] always going to be less than b, which is defined as 1 earlier on?

If x[i] is a positive number, the loop will exit leaving you with 0 in your integral variable.

3. > for(i=0; x[i]<=b; i++)
b is 1, how many times around this loop does it go?

Use a debugger to find out where the code goes. How many times does it execute each loop?

Practice on a file with only 10 lines, not 10000 lines.

Add some printf statements if you can't manage a debugger.
Eg.
printf("Here in loop, counter=%d\n", i );

4. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char* argv[])
{
FILE         *input;
const char    inp_fn[]="cos.dat";
int            i;
double       x[10], f[10], integral;

input = fopen(inp_fn, "r");

for(i=0; i <10; i++)
{
fscanf(input, "%f" "%f", &x[i], &f[i]);
printf(" x= %lf", x);
printf(" f= %lf", f);
}

printf("Here in loop, counter=%d\n", i );

integral=0;

for(i=1; i<10; i++)
{
integral += f[i-1]*(x[i]-x[i-1]);
printf("Here in loop, counter=%d\n", i );
printf("Area = %.4lf \n", integral);
}

printf("Here in loop, counter=%d\n", i );
printf("Area = %.4lf \n", integral);

return(0);
}```
i've changed it to this. and it's printing out x and f as huge numbers which are different in the file but im not sure what's wrong with the fscanf stuff?

5. ok, i changed x,f to x[i] and f[i] and it's giving me smaller but still not correct numbers

6. All your %f's are screwed up in your printf/scanf calls (scanf in particular)
Code:
```\$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function ‘main’:
foo.c:18: warning: format ‘%f’ expects type ‘float *’, but argument 3 has type ‘double *’
foo.c:18: warning: format ‘%f’ expects type ‘float *’, but argument 4 has type ‘double *’
foo.c:19: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
foo.c:19: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘double *’
foo.c:20: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
foo.c:20: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘double *’
foo.c:31: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
foo.c:35: warning: ISO C90 does not support the ‘%lf’ gnu_printf format
foo.c:18: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
foo.c:6: warning: unused parameter ‘argc’
foo.c:6: warning: unused parameter ‘argv’```