1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <iostream>
#include <iomanip>
using namespace std;
//declare Simpson function
void Simpson(double);
int main ()
{
int a,b,n,deltax;
double deltaxdiv;
//prompt user for a,b, and n values
cout<<"Please enter a value for a: ";
cin>>a;
cout<<"Please enter a value for b: ";
cin>>b;
cout<<"Please enter a positive value for n: ";
cin>>n;
if (n<0)
{
cout<<"The value you entered is not positive, please enter again: ";
cin>>n;
}
//display the entered numbers
cout<<"The numbers you've entered are: "<<a<<" "<<b<<" "<<n<<" ";
//calculate for simpsons delta x
deltax=b-(a/n);
deltaxdiv=deltax/3;
cout<<"\ndelta x is: "<<deltaxdiv<<endl;
//call Simpson function
Simpson(deltaxdiv);
cin.get();
return 0;
}
//following is the Simpson function
{
double Simpson(int area);
int x,y,z,sum1,sum2,sum3,area,total;
sum1=0;
sum2=0;
sum3=0;
total=0;
for (x=1; x<n; x=x+2)
{
sum1=sum1+(1/1*(x/2)^5)*4;
}
for (y=1; y<n; y=y+2)
{
sum2=sum2+(1/1*(y/2)^5)*2;
}
for (z=1; z<2; z=z+2)
{
sum3=sum3+(1/1*(z/2)^5);
}
total=sum1+sum2+sum3;
area=pi*(deltaxdiv*total);
cout<<"the final area is: "<<area<<endl;
//call Pi function
double findPi(pi);
return;
}
//following is the Pi function
{
double findPi(double pi);
double indval1,indval2,sumofiterations,pi;
indval1=0;
indval2=0;
for (int i = 1; i <250; i= i + 4)
{
indval1 =indval1+ (1.0 / i);
}
for (int j = 3; j <250; j= j + 4)
{
indval2 =indval2+ (1.0/j);
}
//calculate each set of iterations
sumofiterations=indval1-indval2;
//multiply sum by 4 to receive pi
pi=4*sumofiterations;
//print out answer
cout<<" "<<fixed<<setprecision(6)<<pi<<endl;
return;
}
| |