# Thread: C programming help0 Simpson Rule

1. ## C programming help0 Simpson Rule

HI I am new to C programing and i am trying to do simpson rule in C . But its crashing when I enter a value to the prompt "Enter number of sub-intervals 'N' (must be even):"

Here is the code

Code:
```#include <stdlib.h>
#include <iostream.h>
#include <math.h>
double f(double x); // function prototype
float simpson(float a, float b, int n);		// funt prototype

int main()
{
int n; //
float a,b;
// Program Instructions

scanf("%lf",&a);
scanf("%lf",&b);
printf("Enter number of sub-intervals 'N' (must be even):");
scanf("%d",&n);
if((n%4)!=0)
{
printf("Number of sub-intervals must be a multiple of 4 \n");
exit(1);
}

//printf("a=%.10f",a); printf("b=%.10f",b); printf("n=%.l0f",n0);
float SN; // SN will hold the next value of S
SN = simpson(a, b, n);
printf("Integral=",SN);
return 0;

}

double f(double x)
{
return sin(x);
}

float simpson(float a, float b, int n)
{
n = 2 * n;
float x;
float h = (b - a) / n;
float  S = f(a);

for( int i;i<n-1;i+2)
{
x = a + (h* i);
S = S + (4 * f(x));
}

for( int i;i<n-2;i+2)

{

x = a + (h* i);
S = S + (2 * f(x));
}

S = S + f(b);
//F = (h * S )/ 3;
return ((h * S )/ 3);
}```

2. First of all, you don't want to include "iostream.h" you need <stdio.h> for this. Secondly, maybe scanf is the problem. Sombody?

3. Try compiling with a decent level of warnings.
Code:
```\$ gcc -std=c99 -W -Wall foo.c
foo.c:2:22: error: iostream.h: No such file or directory
foo.c: In function ‘main’:
foo.c:14: warning: implicit declaration of function ‘printf’
foo.c:14: warning: incompatible implicit declaration of built-in function ‘printf’
foo.c:15: warning: implicit declaration of function ‘scanf’
foo.c:15: warning: incompatible implicit declaration of built-in function ‘scanf’
foo.c:15: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘float *’
foo.c:17: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘float *’
foo.c:30: warning: too many arguments for format
foo.c: In function ‘simpson’:
foo.c:50: warning: statement with no effect
foo.c:56: warning: statement with no effect```

4. I would declare a and b to be 'double'.
As Salem pointed out, you probably need to include <stdio.h>

5. O_O As Syscal pointed out...

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double f(double x); // function prototype
float simpson(float a, float b, int n); // funt prototype

int main()
{
int n; //
double a,b;
// Program Instructions

scanf("%lf",&a);
scanf("%lf",&b);
printf("Enter number of sub-intervals 'N' (must be even):");
scanf("%d",&n);
if((n%4)!=0)
{
printf("Number of sub-intervals must be a multiple of 4 \n");
exit(1);
}

//printf("a=%.10f",a); printf("b=%.10f",b); printf("n=%.l0f",n0);
float SN; // SN will hold the next value of S
SN = simpson(a, b, n);
printf("Integral=",SN);
return 0;

}

double f(double x)
{
return sin(x);
}

float simpson(float a, float b, int n)
{
n = 2 * n;
float x;
float h = (b - a) / n;
float S = f(a);

for( int i;i<n-1;i+2)
{
x = a + (h* i);
S = S + (4 * f(x));
}

for( int i;i<n-2;i+2)

{

x = a + (h* i);
S = S + (2 * f(x));
}

S = S + f(b);
//F = (h * S )/ 3;
return ((h * S )/ 3);
}

7. Code:
```or( int i;i<n-1;i+2)
.
.
.
for( int i;i<n-2;i+2)```
These loops are not changing the value of i.

8. maybe you're looking for:

for(blah; blah; i+=2)

9. foo.c:50: warning: statement with no effect
foo.c:56: warning: statement with no effect
THESE !!!!
for( int i;i<n-2;i+2)
have no side effect (the value of i is NOT changed)

I didn't post a bunch of warnings just to feel good, I posted them to make you THINK!

Apparently, you think it's OK just to run code that compiles without syntax errors.

One more thing.
It's [code][/code] around code, not [quote][/quote]