# Thread: Can you spot my mistake?

1. ## Can you spot my mistake?

As part of a homework assignment my program has to have a function which will carry out Simpson's method for integration. Anyway my function is given here:

Code:
```double function1(double a, double b, int N)
{
double result;
double functerm;
double funca = sqrt(1+pow(a,4));
double funcb = sqrt(1+pow(b,4));
int count = 1;
double x=a+((b-a)/N);

printf("%g\n",funca);
printf("%g\n",funcb);
while (x<b)
{
if(count%=2 == 0)
functerm+=2*(sqrt(1+(x*x*x*x)));
else
functerm+=4*(sqrt(1+(x*x*x*x)));

x+=((b-a)/N);
count++;
}
printf("%g\n",functerm);
result=(functerm + funca + funcb)*((b-a)/(3*N));

return result;
}```
When used with the values a=-1, b=1 and N=4, the values for funca and funcb give correct answers, however the result of functerm is a massive number (in the order of 10^238 or something) when it should be about 10. Can anyone spot where my mistake is being created?

The formula for Simpson's rule was given on a sheet by the way, as neither myself nor my maths student friends seem to remember it as its given there. 2. > while (x<b)
Maybe because this can execute 0 times, and leave the result uninitialised. 3. I don't understand.

I thought that was saying that while x was less than b, it would continue to do the loop, and as x increases everytime it would only loop a finite number of times (i.e. until x equals or gets bigger than b). Right? 4. This is dividing by zero.
Code:
`if ( count%=2 == 0 )`
Use parentheses.
Code:
`if ( (count%=2) == 0 )`
And do you really want %=, or just %? 5. So if x > b, how many times does the loop execute?

Why don't you use a debugger to step through the code (or just put a lot of printf() statements in there) to see what route the code takes, and what values are being generated.

When results != expectation, you've found a bug. 6. Originally Posted by Dave_Sinkula
This is dividing by zero.
Code:
`if ( count%=2 == 0 )`
Use parentheses.
Code:
`if ( (count%=2) == 0 )`
And do you really want %=, or just %?
Nope. That makes no difference to the answer. I thought that what I'd written there was count (which should increase by one every loop) divided by 2 and giving the remainder (i.e. a check for even numbers). 7. Originally Posted by Salem
So if x > b, how many times does the loop execute?

Why don't you use a debugger to step through the code (or just put a lot of printf() statements in there) to see what route the code takes, and what values are being generated.

When results != expectation, you've found a bug.
I thought I'd defined x to be less than b in the first place (as long as a was less than b, which being the lower limit of the integral it will be).

But if x was greater than b the loop wouldn't run would it, so it skip the code block, correct? 8. I have no idea what (count%=2) is all about. Never seen it before.

Are you sure you don't want to use:
Code:
`if((count % 2) == 0)`
? 9. check what salem said. Initialise that term to zero at the start of the code and check if it is changing at all in the first place.

>I have no idea what (count%=2) is all about. Never seen it before.
it caught me a little off gaurd too.
that is something like " sum+=2 " right?
it means,
count = count %2 10. It appears that a divison by zero is happening, as N isn't being defined for some reason.

Surely
Code:
```printf("Please enter the number of intervals over which the integration is to be made (must be even and positive)\n");
scanf("%d",&N);```
should define the value of N right? Where N is an integer. 11. Originally Posted by kris.c
check what salem said. Initialise that term to zero at the start of the code and check if it is changing at all in the first place.

>I have no idea what (count%=2) is all about. Never seen it before.
it caught me a little off gaurd too.
that is something like " sum+=2 " right?
it means,
count = count %2
Ok, so he's modifying count ON PURPOSE, and THEN making a checking of that value against 0.
I thought he wanted a check of the remainder of count, only. Interesting. 12. may be you are not passing it correctly to the function 13. My next stage has to been to ask for and define the value of N within the function itself rather than relying on passing it across from the main code. Yet its still not being defined correctly. My code is thus:
Code:
```printf("Enter number of intervals\n");
scanf("%d",&N);```
where N is defined as an integer. Surely this should ask for a value for N and then store it within N, yes?

This program is doing my head in! 14. Seems OK so far. Popular pages Recent additions 