So I know a do while loop runs at least once but how can you make it run at least twice, as I was told in my assignment that I might need to make a loop that runs at least twice? I really have no idea on this one
#include <iostream>
#include <math.h>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
usingnamespace std;
double f(double x);
int main(){
double sum,SumOld,x,eps,dx,a,b;
int n;
cout<<"\n a";
cin>>a;
cout<<"\n b";
cin>>b;
cout<<"\n Error";
cin>>eps;
n=1;
sum=0;
do
{
sum=0;
x=a-(dx/2);
dx=(b-a)/n;
SumOld=sum;
x=x+dx;
n=2*n;
sum=sum+f( x )*dx;
}
while(fabs(SumOld-sum) < eps );
cout<<"\n The integral is approximated to "<<sum <<" with a middlesum of "<<n/2<<" intervals";
return 0;
}
double f(double x)
{
double resultat;
resultat=x*x;
return resultat;
}
I tried doing the following in the while() (fabs(SumOld-sum) < eps) < 2. but then I dont get the final output. I want to find the area under a curve given two limits a and b where the program asks for the error allowed. I think most of the code is correct, but I need to make it loop twice at least
How are u working out the error. The exact value for the area is (b^3 - a^3)/3. So you work out the area in one loop, calculate the exact value separately take the difference.
The other way of calculating is running through 2 or more while loops separately which would indicate a function(). From these an estimate of the error can be made.
Or you can run the loop with varying slice arrangements and see whether the results converge so you can work out some sort of error function based on the exact value.
kemort I am supposed to write the error I choose. So if err=0.0000001 this should be very close to the exact value using the integral function (which i'm not allowed to use). Exactly how would I make it using several while loops. Would it be a do while while... etc. I did the following