Forcing loop to run at least twice

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    int counter  = 0;
    int topCount = 0;
    cout << "enter top count: ";
    cin  >> topCount;
    do
    {
        cout << "loop " << counter << endl;
        counter = counter + 1;
    }while (counter < 2 || counter < topCount);  // do at least two loops else do topCount loops

    return 0;
}

Okay a little bit confused now I want to make the following loop twice

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
  #include <iostream>
#include <math.h>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
using namespace 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
1
2
3
4
5
6
7
8
9
10
11
12
int main(){
int counter  = 0;

do{

//whatever you have


 counter = counter + 1;
while((fabs(SumOld-sum) < eps ) or (counter < 2))

closed account (48T7M4Gy)
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.

My guess is the first is best but it's up to you.
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

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
  #include <iostream>
#include <math.h>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
using namespace std;

double f(double x);


int main(){

		double sum,SumOld,x,eps,dx,a,b;
		int n,counter;

		cout<<"\n a";
		cin>>a;

		cout<<"\n b";
		cin>>b;

		cout<<"\n Error";
		cin>>eps;

		n=1;
		sum=0;

		counter=0;

		do
				{
			sum=0;
					x=a-(dx/2);
					dx=(b-a)/2;
					SumOld=sum;
						x=x+dx;
						n=2*n;
						sum=sum+f( x )*dx;
						counter=counter+1;

}

				while((fabs(SumOld-sum) < eps) or (counter<2));
		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;
}


but it gives a wrong result
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/175093/
http://www.cplusplus.com/forum/beginner/174901/
Last edited on
Yeah thats the assignment I'm working at. I should probably have deleted the previous one
?
Topic archived. No new replies allowed.