# Thread: i am getting infinity in my program !!! ??

1. ## i am getting infinity in my program !!! ??

this is what i am suppose to do.:
Scan two doubles a and b (a<b).
Integrate numerically y=0.5*cos^2(x)+0.25 from x=a to x=b.

Use two do/while statements to continue the
calculations for different n_trap and different a and b. For
example, use ask=1 to continue calculations for different n_term
within the inner do/while loop, and ask=0 to exit that loop.
Use flag=1 to continue calculations for different a and b
within the outer do/while loop, and flag=0 to exit that loop.

and this is my program so far. btw im only doing the first part havent tried the flag part because im confused

#include <stdio.h>
#include <math.h>
main ()
{
double a, b, y, del_x, Integral, x, sum;
printf("Scan a and b:");
scanf("%1f %1f %1f", &a, &b);
do
{
printf("Enter the number of trapezoids:\n");
scanf("%d", &n_trap);
del_x = (b-a)/(double)n_trap;
printf("n_trap =%d\t del_x =%g\n", n_trap, del_x);
x=a;
y=0.5*cos(x)*cos(x)+0.25;
sum= -0.5 * y;

for(k=0; k<=n_trap; k++)
{
x=a+k*del_x;
y=0.5*cos(x)*cos(x)+0.25;
sum += y;
}
sum -= 0.5*y;
sum += del_x;
printf("Integral = %f\n", sum);
printf("Would you like to continue with another n_trap? y=1/n=0\n");
}
}

2. First off, you want code tags. Read the sticky "Posting Code?? Read This First!!" (I should make it a link, but I'm too lazy.)

You also want %lf, not %1f.

3. okay sorry i forgot about that. but yea im not much of a programmer so this is all new to me

4. okay so i notcied what i did wrong.
okay so short and sweet. my answer comes out to be Integral = 49.661105
and im trying to match the answer we were given which is2.23272
any ideas?

5. Another pro tip would be to not have three %lf in your format string if you only intend to read two numbers. You are also supposed to multiply your final answer by delta x, not add it.

6. yea i changed that sutff and its working. now im gonna start the second loop/ hopefully it goes well