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):"
please help
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
printf("\n\nPlease enter lower integration limit:");
scanf("%lf",&a);
printf("Please enter upper integration limit:");
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);
}