for,if,even,max,min

hey lads,

Hope you can help me, I have to do my HW as below:

Solve a+b-c for "n" times, following the conditions:
1. Calculate the Sum of all the Results.
2. Calc sum of results for even and odd iterations.
3. Calc sum of even results, for odd iterations.
4. Calc sum of odd results, for even iterations.
5. From iteration 2 to iteration 5, solve a+b-c, where a,b,c are values included in x...y interval.
6. For all the even results find out the maximum and minimum answer, if the iteration is smaller then the result ( i<R ).
7. Solve the equation a+b-c, for all pair of answers ( r1+r2, r3+r4...rn+rn).

That's what i've done so far, anyway it doesn't calculate what i have to, and even more when i run the app it shows an infinte nr. of answers, I am stuck :(

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
  int n, s=0, sip=0, sii=0,sipri=0, siirp=0,x,y;

  /*1. Calculate the Sum of all the Results.
2. Calc sum of results for even and odd iterations.
3. Calc sum of even results, for odd iterations.
4. Calc sum of odd results, for  even iterations.*/

     cout<<" Enter n=";
     cin>>n;
     for(int i=0,a,b,c, R;   i < n   ;  i++  )
     {
        a=rand()%10;
        b=rand()%10;
        c=rand()%10;
        R=a+b-c;
        s+=R;
         if( i%2==0 ) sip+=R;
          else sii+=R;
          if( i%2==0 && R%2!=0 ) sipri+=R;
          else if( i%2!=0 && R%2==0 ) siirp+=R;
        cout<<" i="<<i<<" -  a="<<a<<" b="<<b<<" c="<<c<<" a+b-c="<<a+b-c<<endl;
   }
       cout<<endl;
       cout<<"   s="<<s<<endl;
       cout<<" sip="<<sip<<endl;
       cout<<" sii="<<sii<<endl;
       cout<<" sipri="<<sipri<<endl;
       cout<<" siirp="<<siirp<<endl;
       
       //Solve the equation a+b-c, for all pair of answers

       for(int i=0,a,b,c, R;   i < n   ;  i+=2  )
       R=a+b-c;
       cout<<"R= "<<endl;
       /// From iteration 2 to iteration 5, solve a+b-c, where a,b,c are values included in x...y interval.
       
             cout<<"Enter x, y, where y>x "<<endl;
        cin>>x;
        cin>>y;
        cout<<"x= "<<x<<" y= "<<y<<endl;
        
              for (int a,b,c,t, i= rand()%3+2; i<5; i++) {
     	t=y-x;
     	a= rand()%t+x;
     	b= rand()%t+x;
     	c= rand()%t+x;
    
	 a+b-c; 
	 
	 cout<<" i="<<i<<"a= "<<a<<" b= "<<b<<" c= "<<c<<" a+b-c= "<<a+b-c<<endl;
	 }   
 //For all the even results find out the maximum and minimum answer, if the iteration is smaller then the result ( i<R )
 
 for ( int R, max=INT_MIN, i=0   ; i<R ; max<R  ) {
 	if (max<R && R%2==0)
 max=R;
 cout<<" max = "<<max<<endl;
 }
 for (int R, min=INT_MAX, i=0 ; i<R ; min>R  ){
 	if (min>R && R%2==0)
 min=R;
 cout<<" min = "<<min<<endl;
 
 }
   return 0;
Might be a good idea to post all your program.
Can you post the full text of the assignment? That might help answer some questions like:
From iteration 2 to iteration 5, solve a+b-c, where a,b,c are values included in x...y interval.
That seems to say "evaluate a+b-c where a, b, and c take on all possible values in the interval [x,y]. Is that correct?
7. Solve the equation a+b-c, for all pair of answers ( r1+r2, r3+r4...rn+rn).
It's not clear what this means. is a "pair of answers" (r1,r2), or (r1+r2, r3+r4)? Either way, it sounds like we're supposed to plug the two numbers into two variables and solve for the third, but plug it into which two? a & b? a & c?

Also, you're most likely to get help if you post code that will compile, not just a fragment.
Lines 24-28: If someone runs the program, I doubt they know what s, sip, sipri etc. are. You should give more descriptive text.

Parts 5 & 6 sound like they should be done for each iteration (1-N)
sorry for late answer,

sip - is the sum for even results.
sii - is the sum for odd res.
sipri- is the sum for even res and odd iterations
siirp- is the sum for odd res. and even iterations.

"evaluate a+b-c where a, b, and c take on all possible values in the interval [x,y]. Is that correct?
- yes it is correct.

Solve the equation a+b-c, for all pair of answers ( r1+r2, r3+r4...rn+rn)
- means to evaluate the sums for iteration 1+ iteration 2, then iteration 3+ iteration 4 and so on. Like in pairs.

Given task was :

/*1. Calculate the Sum of all the Results.
2. Calc sum for all the results for each even and odd iterations.
3. Calc sum for all even results, just for odd iterations.
4. Calc sum for all odd results, just for even iterations.
5. Evaluate a+b-c where a, b, and c take on all possible values in the interval [x,y]
6. For all the even results find out the maximum and minimum result values, if the iteration is smaller then the result ( i<R )
7. evaluate the sums for iteration 1+ iteration 2, then iteration 3+ iteration 4 and so on. Like when you run the app it shows the sum for iteration 1 and 2, another result for iteration 3 and 4 and so on for each pair. */
Time to close this thread max. You’ve ignored 2 separate requests for complete code.
5. From iteration 2 to iteration 5, solve a+b-c, where a,b,c are values included in x...y interval.
It doesn't make sense to print a+b-c for all possible values from x to y. That would print a large table in each iteration from 2 to 5 and tables would be the same. So I think this really means 5. From iteration 2 to iteration 5, solve a+b-c, where a,b,c are [i]random values in the interval [x...y].[/i]

I coded it up that way. Here is my output. For n=4, x,y = 10,20. Does this look right to you? I want to make sure I understand the problem before giving advice on the solution:
Enter number of iterations n=4
Enter interval bounds x and y: 10 20

Iteration 1. 3+3-2=4

Iteration 2. 9+0-8=1
5. Values of a,b,c from 10 to 20
        11+19-18=12
7: Sum of previous 2 results: 4+1=5

Iteration 3. 9+1-1=9
5. Values of a,b,c from 10 to 20
        12+14-17=9

Iteration 4. 3+0-6=-3
5. Values of a,b,c from 10 to 20
        16+18-11=23
7: Sum of previous 2 results: 9+-3=6

Results:
1. Calculate the Sum of all the Results: 11
2. Calc sum of results for even and odd iterations:
        Even iterations: -2
         Odd iterations: 13
3. Calc sum of even results, for odd iterations: 4
4. Calc sum of odd results, for even iterations: -2
6. For all the even results find out the maximum and minimum answer,
   if the iteration is smaller then the result ( i<R ):
         max=4  min=4


@dhayden you explained better then me, you understood everything perfectly. Sorry for late answer, I was on a holiday with my family.
Last edited on
For 6. guess, I wrote the correct code,
For all the even results find out the maximum and minimum answer,
if the iteration is smaller then the result ( i<R ):


1
2
3
4
5
6
	if(i%2==0&&i<R&&max<R) 
              {SE+=R;
		max=R;}
		if(i%2==0&&i<R&&min>R) 
              {SE+=R;
		min=R;}


When i run the app it shows all (n) the correct max and min values if i<R,

Last edited on
MaxGreen,

I think you have #6 wrong. It's for all even results, not for all even iterations. So, for example, the code for max should be :
1
2
3
if (R%2 == 0 && i<R && max < R) {
    max = R;
}

FWIW, I did it like this:
1
2
3
4
5
6
7
        // 6. For all the even results find out the maximum and
        //    minimum answer, if the iteration is smaller then the
        //    result ( i<R ).
        if (R%2 == 0 && i < R) {
            if (R < minR) minR = R;
            if (R > maxR) maxR = R;
        }



@dhayden thanks,


how can i calculate the sum for n iterations for the pair of answers

ex:

i0 - r=a+b
i1 - r1=a+b
i3 - r2=a+b
...
in - r(n)=a+b
____
R(pair)= i0 + i1;
R(pair2)= i2 + i3;
....
R(pairn) = i(n) + i(n+1);


For example the sum of the each result is the code below, where a,b,c are rand values from 0-9.
1
2
3
4
5
6
7
8
 for(int i=0,a,b,c,R; i < n; i++)
     {
        a=rand()%10;
        b=rand()%10;
        c=rand()%10;
        
        R=a+b-c;
        S+=R;
Last edited on
I did it like this:
1
2
3
4
5
6
7
8
9
int lastR = 0;  // previous R value
for (int i=0; i<=n; ++i) {  // Note that first iteration is odd.
    ...
        // 7. Solve the equation a+b-c, for all pair of answers ( r1+r2,
        //    r3+r4...rn+rn).
        if (i % 2 == 0) {
            cout << "7: " << lastR << '+' << R << '=' << R+lastR << '\n';
        }
        lastR = R;
@dhayden thank you very much indeed sir!
Topic archived. No new replies allowed.