TechArena Community Something is wrong with my C++ code. Plz help

 Tags:

# Something is wrong with my C++ code. Plz help

## Software Development

#1
26-10-2011
 Member Join Date: Oct 2011 Posts: 3
Something is wrong with my C++ code. Plz help

I am trying to use the Simpson's Method to calculate area under the curve using this function 2x^2+x but its not working?

Can some one help me and fix the problem? Its using Simpson's Method and want user to input a,b and N then calculate the area of the function given 2x^2+x

[code]
#include <stdio.h>
#include <conio.h>
#include <cstdlib>

double simpson(double a, double b, int n);
double fk(double x);
//**********************************
int main()
{

double a,f,b;
int n;
//Duomenys----------------
printf("Enter value of a, example a=0\n a:");
scanf("%lf", &a);
//------------------------
printf("Enter value of n, example n=7\n n:");
scanf("%d",&n);
printf ("Enter Value of b, example b=1\nb:");
scanf("%lf", &b);
f=simpson(a ,b , n); // Integralas
//Rezultatas----------------
printf("The result is f=%7.2f\n",f);

}
//**********************************
double simpson(double a, double b, int n)
{
double c= (a+b)/2.0;
double h3= abs(b-a)/6.0;
double result= h3*(fk(a)+4.0*fk(c)+fk(b));
return result;

}
//**********************************
double fk(double x)
{
return x * x;
}
/[code]

#2
27-10-2011
 Member Join Date: Mar 2011 Posts: 152
Re: Something is wrong with my C++ code. Plz help

Are you sure its a C++ code. according to my knowledge its a C code. please correct me if i am wrong...