if/for/while do

pals,
have to write a code which:

Prints out for "n" nr. of times ,
the last result of the equation a+b-c ,
if the sum of all the results is bigger then 10 and smaller then 50.
Where "n" is set up by the user.

1
2
3
4
5
6
7
8
9
do{
   if (s>10 && s<50) //if the sum of all the results is bigger then 10 and smaller then 50.
   for(int u=0; u<=n; u++)
   cout<<"Last answer was"<<rez<<endl; //the last result of the equation a+b-c
   cout<<" Print the last answer for n times, input n"<<endl;
   cin>>n;
}
while(s>10 && s<50);   
      

and I am stuck, because I can't understand how the sum of the results (s+=rez) can be printed out for n times.
what do you want to print, is it 's' ?

something like
for(int u = 0; u<n && s>10 && s<50; u++) //you can move the if condition into the loop.
cout << s;
Last edited on
@jonnin,
what do you want to print, is it 's' ?
- yes, to print "s" for "n" number of times, where the user inputs "n";(user decide how many times "s" should be printed out)
Last edited on
Is this part of the same homework as here: Is this part of the same homework as here: http://www.cplusplus.com/forum/beginner/271719/ ?

Can you post the exact assignment. The wording may matter. I think the key is whether you need to store all the results before you decide what to print.

It sounds like the requirement is:
add up the results of the n calculations.
Then, after the loop, if the sum of results is between 10 and 50, then print the value of the last result (the one from N'th iteration).
@dhayden, hey mate, how are you?

that's another example, we pass the while and do while loop so now the purpose is a little different.

that's the full code
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
    

    int n,s=0;
       cout<<" Enter n=";
       cin>>n;

    for(int i=0,a,b,c,rez,j,k=0; i < n ; i++)
      {
        do{
           a=rand()%20;
   }while(a%2!=0);
  
        do {b= rand()%100;
        k++;
   }while ( b < 50 && b > 70 );

       cout<<"b got incorrect values for "<<k<<endl;
       
       c=rand()%20;
       
       rez=a+b-c;
       
       s+=rez;
      //
 
    for(int u = 0; u<n && s>10 && s<50; u++) //you can move the if condition into the loop.
       cout << "S for "<<s<<" times"<<endl;
//

      cout<<setw(4)<<i+1<<" a="<<a<<" b="<<b<<" c="<<c<<" a+b-c="<<rez<<endl;
      
          j=0;
    while(rez%2==0 && j<3)
          {
            a=rand()%100+100;
            b=rand()%100+100;
            c=rand()%100+100;
            cout<<"\t "<<setw(4)<<j+1<<" a="<<a<<" b="<<b<<" c="<<c<<" a+b-c="<<a+b-c<<endl;
          j++;
          }
          }
    
      cout<<" s="<<setw(3)<<s<<endl;
   return 0;
  }


i have to edit it to the final version, but that's what i've done so far.

the conditions are below :

1. solve a+b-c for "n" number of times.
2. If the result is even, add 3 more iterations of a+b-c, where a,b,c have values from 0-100.
3. variable a should receive only even values.
4. If the Sum of the results is bigger then 10 and smaller then 50, the last solved result should be printed out for "n" number of times. Note that "n" number of times is set up by the user(operator cin).
5. variable b should receive random values from 0-100, where just values from 50-70 will be considered as correct values. We should count how many times variable b received incorrect values( incorrect are the ones beside the interval 50-70, as initially variable b had the interval 0-100)
Last edited on
I have issue with requirement #2.
2. If the result is even, add 3 more iterations of a+b-c, where a,b,c have values from 0-100.
:(
Since the result is likely to be even half the time... And then we add three more iterations... The loop can run forever.

Last edited on
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
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
    srand(time(NULL));
  
  int n, a, b, c, res, badB = 0;
  
  cout << "Enter n: ";
  cin >> n;
  
  
  for(int i = 0; i < n; i++) {
      a=rand()%50;
      a*=2; //a can only be even number
      b=rand()%100;
      while(b<50 || b>70) {
          badB++; //tells when b is incorrect value;
          b=rand()%100;
      }
      c=rand()%100;
      res = (a+b-c);
      cout << i + 1 << ": " << a << " + " << b << " - " << c << " = " << res << endl;
     /* if(res%2 == 0) {
          n+=3; //don't do this.
      }*/
  }
  cout << "b had " << badB << " incorrect values." << endl;
  
   return 0;
  }


I don't have requirement 4 either. After running the program a few times, the odds of hitting that number between 10 and 50 aren't good. If you do fall in that range on the first iteration, then you are lucky, but it is lost by the second iteration almost always...
@ thank you Manga, much help.

The main problem that I am facing right now is when should I use the loop while, or do while or the loop for, since I have just a few days since I 've finished learning these subject.
I guess i need practice, for now , these different loops seem to be a little unclear at the moment... ((
Last edited on
The 'for' loop works well if you want a loop to run a set number of times.

The 'while' loop will keep going forever as long as a certain condition is true. If the tested condition is not true then the loop ends. Also if the condition is never true then the loop never happens. Not even once.

The 'do while' loop is like the while loop except it will run through at least one time even if the condition is never true.
@ thanks Manga, really appreciate it, need practice in order to get over and easily use them
Hey Max!

I have to say that this is a seriously crazy assignment. The requirements and the output are very vague. As Manga points out, the loops can (and for my first version it did) go on forever. Because I have no respect for the assignment, I'll post my code.

A useful technique to doing assignments like this is to copy the requirements into the code as comments. Then just move the comments around to where you implement them with code.

Here is my program. See the questions on the requirements. In several runs, I never saw a sum of results between 10 and 50, so requirement #4 never triggered. Maybe when b is incorrect, the sum result should not be added to the sum?
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
// 1. solve a+b-c for "n" number of times.

// 2. If the result is even, add 3 more iterations of a+b-c, where
// a,b,c have values from 0-100.

// 3. variable a should receive only even values.

// 4. If the Sum of the results is bigger then 10 and smaller then 50,
// the last solved result should be printed out for "n" number of
// times. Note that "n" number of times is set up by the user(operator
// cin).

// 5. variable b should receive random values from 0-100, where just
// values from 50-70 will be considered as correct values. We should
// count how many times variable b received incorrect values(
// incorrect are the ones beside the interval 50-70, as initially
// variable b had the interval 0-100)


#include <iostream>
#include <cstdlib>

using std::cin;
using std::cout;
using std::srand;
using std::rand;

int main()
{
    int n;			// user input number of iteratinos
    int limit;			// actual number of iterations
    int a,b,c;
    int incorrectB=0;		// number of times B is incorrect
    int nthResult;		// result of the nth iteration
    int sumOfResults = 0;

    srand(time(0));
    
    cout << "Enter the number of iterations n: ";
    cin >> n;

    limit = n;
    // Loop 1 to n so the first iteration is odd, second is even, etc.
    for (int i=1; i<= limit; ++i) {
	// 3. variable a should receive only even values.
	// Q: Even values from 0 to 100?
	a = rand() % 51 * 2;

	// 5. variable b should receive random values from 0-100, where just
	// values from 50-70 will be considered as correct values. We should
	// count how many times variable b received incorrect values(
	// incorrect are the ones beside the interval 50-70, as initially
	// variable b had the interval 0-100)
	// Q: When teh value is correct, do we do the other calculations?
	b = rand() % 101;
	if (b<50 || b>70) {
	    ++incorrectB;
	}

	// Q: What are legal values for c? 0-100?
	c = rand() % 101;
	
	// 1. solve a+b-c for "n" number of times.
	int result = a+b+c;
	
	cout << "R1. Iteration " << i <<'\t'
	     << a << '+' << b << '-' << c << '=' << result << '\n';

	// 2. If the result is even, add 3 more iterations of a+b-c,
	// where a,b,c have values from 0-100.
	// Q: I assume you only add 3 once. Otherwise the limit grows
	// without bounds.
	if (result%2 == 0 && limit == n) {
	    limit = n+3;
	    cout << "\tR2. Limit increased to " << limit << '\n';
	}

	// Remember the result of the n'th iteration for requirement #4
	// (see below). You can't just keep the last result because requirement
	// 2 increases the iteration count.
	if (i == n) {
	    nthResult = result;
	}

	sumOfResults += result;
    }	

    // 4. If the Sum of the results is bigger then 10 and smaller then 50,
    // the last solved result should be printed out for "n" number of
    // times. Note that "n" number of times is set up by the user(operator
    // cin).
    cout << "4. sum of results = " << sumOfResults << '\n';
    if (sumOfResults > 10 && sumOfResults < 50) {
	cout << "4. N'th iteration result = " << nthResult << '\n';
    }
}

Enter the number of iterations n: 3
R1. Iteration 1 68+65-23=156
        R2. Limit increased to 6
R1. Iteration 2 88+4-70=162
R1. Iteration 3 14+86-64=164
R1. Iteration 4 88+44-50=182
R1. Iteration 5 44+24-95=163
R1. Iteration 6 90+84-58=232
4. sum of results = 1059


Enter the number of iterations n: 5
R1. Iteration 1 76+28-53=157
R1. Iteration 2 84+30-8=122
        R2. Limit increased to 8
R1. Iteration 3 18+45-44=107
R1. Iteration 4 96+42-28=166
R1. Iteration 5 80+81-26=187
R1. Iteration 6 98+73-12=183
R1. Iteration 7 70+39-73=182
R1. Iteration 8 8+76-90=174
4. sum of results = 1278


Enter the number of iterations n: 5
R1. Iteration 1 76+83-18=177
R1. Iteration 2 84+5-21=110
        R2. Limit increased to 8
R1. Iteration 3 2+88-82=172
R1. Iteration 4 30+30-77=137
R1. Iteration 5 58+56-49=163
R1. Iteration 6 18+81-19=118
R1. Iteration 7 76+49-26=151
R1. Iteration 8 56+2-46=104
4. sum of results = 1132

Last edited on
@ dhayden, thank you so much. Right now our class is talking about your code, since it has a strong point of truth
Re-reading the requirements and the code, I see at least one problem. Kudos to anyone in your class who can spot it. Here's a hint: Look at the requirements and the output (not the code). Does the output show that I've met all the requirements?
Maybe the 4th requirement does not mean the sum of all the results, but each individually. And for each one that falls in the target range, output that result again… Maybe?
Topic archived. No new replies allowed.