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;
}