# Thread: Need help with C++ integration problem

1. ## Need help with C++ integration problem

I have an assignment where I have to use the trapezoidal and simpson's rules to estimate the integral of a function. Everything works as far as I can tell, except for the final results. If someone could help me out, it would be much appreciated.

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

//Declare global variables
double A, B, x=0, n1, n2;
char ch;

//Function to evaluate both of the functions
double evalf(double x){
if(ch == 'a')
return(sin((2*x)/5) - x + 1);
else
return(cos((2*x)/5) + (3*x) + 1);
}

//Function to input the number of intervals wanted for the Trapezoidal rule,
//and see if their accuracy is acceptable.
double trap_intervals(){
double accuracy=0, error=1, h, n;
while(error > accuracy){
printf("\nPlease enter the number of subintervals you would like: ");
scanf("%lf", &n);
printf("Please enter the accuracy you would like: ");
scanf("%lf", &accuracy);
h = (B-A)/n;
error = ((B-A)*(h*h)*sin(2*B/5))/75;
if(error < 0)
error = -error;
if(error > accuracy)
printf("\nSorry, you do not have enough subintervals for the level of accuracy.");
}
return(n);
}

//Function to estimate the integral of the function using the Trapezoidal Rule
double trapezoid(){
double X, Y, p, h, x, sum;
h = (B-A)/n1;
X = evalf(A);
Y = evalf(B);
for(x=(A+h); x<B; x+=h){
p = evalf(x);
sum+=p;
}
return(h*((1/2)*X + sum + (1/2)*Y));
}

//Function to input the number of intervals wanted for Simpson's rule, and see
//if their accuracy is acceptable.
double simp_intervals(){
double accuracy = 0, error = 1, h, n;
while(error > accuracy){
printf("\nPlease enter the number of subintervals you would like: ");
scanf("%lf", &n);
printf("Please enter the accuracy you would like: ");
scanf("%lf", &accuracy);
h = (B-A)/n;
error = (-4*(B-A)*(h*h*h*h)*sin(2*B/5))/28125;
if(error<0)
error= -error;
if(error > accuracy)
printf("\nSorry, you do not have enough subintervals for the level of accuracy.");
}
return(n);
}

//Function to estimate the integral of the function using Simpson's Rule
double simpson(){
double X, Y, x, p, q, h, sum1, sum2;
int i;
h = (B-A)/n2;
X = evalf(A);
Y = evalf(B);
for(x=A+h; x<B; x+=h){
i = 1;
if(i%2 == 0){
p = 2*evalf(x);
sum1+=p;
}
else{
q = 4*evalf(x);
sum2+=q;
}
i++;
}
return((h/3)*(X + sum1 + sum2 + Y));
}

//Main function starts here.
int main(){

double temp;

//Decide which function you wish to integrate
printf("If you want to integrate the function f(x) = sin(2x/5) - x + 1, press 'a'.");
printf("\nIf you want to integrate the function f(x) = cos(2x/5) + 3x + 1, press 'b'.\n");
scanf("%c", &ch);

//Ensure the user only picks a or b
while(ch != 'a' && ch != 'b'){
printf("\nSorry, invalid input. Please try again:\n");
scanf("%c", &ch);
}

//Tell the user which function will be integrated
if(ch == 'a')
printf("\nThe function we are integrating is f(x) = sin(2x/5) - x + 1.\n");
else
printf("\nThe function we are integrating is f(x) = cos(2x/5) + 3x + 1.\n");

//Choose the intervals, swap A and B if A is greater than B
printf("Please enter the values of the interval [A,B].\nA: ");
scanf("%lf", &A);
printf("\nB: ");
scanf("%lf", &B);
if(A>B){
printf("\nA is larger than B. Swapping values now...");
B = temp;
A = B;
temp = A;
temp = 0;
}

//Pick intervals for Trapezoidal Rule
printf("\nWe must now enter the number of intervals for the trapezoidal rule.");
n1 = trap_intervals();

//Pick intervals for Simpson's Rule
printf("\nWe must now enter the number of intervals for Simpsons rule.");
n2 = simp_intervals();

//Estimate the value of the integration using the Trapezoidal Rule
temp = trapezoid();
printf("\nThe value of integration using the trapezoidal rule is %lf.", temp);
temp = 0;

//Estimate the value of the integration using Simpson's Rule
temp = simpson();
printf("\nThe value of integration using Simpson's rule is %lf.", temp);

//End program
printf("\n\nPress any key to finish...");
getch();
}```

2. Basically the problem is in the trapezoid and simpson functions, sorry for pasting the whole code.

3. What C++? All I see is C!

4. First, learn to write code without unnecessary Global Variables.
Second, learn to swap two value correctly.
Third, learn to post C code in the C sub-forum.

Code:
```   if(A>B){
printf("\nA is larger than B. Swapping values now...");
B = temp;
A = B;
temp = A;
temp = 0;
}```
Tim S.