# I have a problem in finding the exact area and estimating the error [closed]

The program should first read from the keyboard the values of a and b then estimate the area under f(x) in the interval [a, b] using Simpson’s 1/3rd rule and display the estimated area and the error in the estimation. Which that f(x)=sqrt(x+1)+0.25, I have an syntax error in data array and this function:

float f(float data[])
{
return(sqrt(data[]+1)+0.25);
}


The rest of my code is:

#include<iostream>
#include<cmath>

using namespace std;

//prototype function
float GenerateData(float a ,float b , int n, float data[]);
float f(float data[]);
float Simpson(float a, float b, int n,float data[],float I, float J,float simpson);
float g(float data[]);

//area function
float f(float data[])
{
return(sqrt(data[]+1)+0.25);
}

// function after ingreation
//float g(float data[])
//{
//    return((2/3)*sqrt(pow(data[]+1),3)+(0.25*data[])+n);
//}

int main()
{
//decleration
float a,b,simpson;
float data[100], I =0, J=0 ;
int n;

//input a b and interval
cout<<"given f(x)=sqrt(x+1)+0.25 "<<endl;

if (a>b)
{
cout<<"error";
}
cout<<"please enter lower limit ";
cin>>a;
cout<<"please enter upper limit ";
cin>>b;

cout<<"please enter the number of intervals ,even number";
cin>>n;
if ( n%2!=0)
{
cout<<"error";
}

//output the estimating area
cout<< "the estimating area using simpson's rule "<<Simpson( a,  b,  n, data[], I, J,simpson);

//output the exact area
//cout<<"the exact area "<<

//output the error
//cout<<"The Total Error is : "<<endl;

return 0;
}

//function generate data
float GenerateData(float a ,float b , int n,float data[])
{
float xi;
//using loop
for(int i=0;i<n+1;i++)
{
data[i]=xi=a+i*(b-a/n);
}

return data[];
}

// function simpson's rule
float Simpson(float a, float b, int n,float data[],float I, float J,float simpson)
{
float A;
//loop
for(int i=1;i<n;i++)
{
if (i%2!=0)
{
I=I+f(data[i]);
}
}

//loop
for(int i=2;i<n-1;i++)
{
if (i%2==0)
{
J=J+f(data[]);
}
}

simpson=(b-a/n*3)*(f(a)+(4*I)+(2*J)+f(b));
cout<<"The Value of integral under the enterd limits is by using simpson's rule: "<<endl;
cout<<simpson<<endl;

return A;
}

• Hi fatma. I have attempted to properly format your code so that it is more legible. When including code snippets in your question you cannot just copy and paste your code to have it formatted properly. You need to put 4 spaces before writing code to get it to display as a light blue code block.. Oct 4 '15 at 4:44
• This question was previously closed and while some improvements have been made, I believe it is now off-topic as it appears to be about writing correct c code and not computational science. I have included an answer to help you, but I think this question will again be closed. Oct 4 '15 at 4:58
• I'm voting to close this question as off-topic because it is purely about C programming. Oct 4 '15 at 9:06

## 1 Answer

The following is not valid C:

return(sqrt(data[]+1)+0.25);


I think what you want is:

float f(float data[], int n)
{
float fx = 0.0;
for(int i=0;i<n;i++){
fx += sqrt(data[i]+1)+0.25;
}

return fx;
}


where the integer input n is the size of your data array.

Edit

On re-reading the question I think what you want is the following function:

float f(float x)
{
return sqrt(x+1)+0.25;
}


where f is called in your simpson function body (and elsewhere) like:

for(int i=1;i<n;i++){
if(i%2!=0){
I = I + f(data[i]);
}
}


Note 1: There may be problems with your understanding of how to implement simpsons rule - which would make this question on-topic - however I think you need to first get a better understanding of how to write C code before posting a question on this stack exchange. Currently with the improper c code it makes it hard to distinguish if you are having difficulty with understanding how to implement simpsons rule or if its just more coding experience that you need. Good luck and I hope these hints help.

Note 2: You can find the exact area because the integral of the function f(x)=sqrt(x+1)+0.25 can be computed (pen & paper) exactly. Once you have your exact area and your approximate numerical area they can be compared to find the error in your approximation. The exact area is:

$Area_{exact} = \int_{a}^{b}\sqrt{x+1}+0.25 dx = (\frac{2}{3}(x+1)^{3/2}+0.25x)|_{a}^{b}$