Maximum odd and even value & Minimum odd and even value

Hey guys,

I have to write a code which will calc. which is the Maximum odd and even value & Minimum odd and even value.

That's what i've done so far, pls help to finish the code. Have no clue

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
     #include <iostream>
  #include <cstdlib>  
  #include <ctime>

  using namespace std;
  

  int main()
  {
  	srand(time(0));
  	
 int a, b, c, n, i=0;
 int max=INT_MIN;    
 int min=INT_MAX;
 
 cout<<"Enter n= ";
 cin>>n;
 
 E: 
 a=rand()%20;
 b=rand()%20;
 c=rand()%20;
 
 int R=a+b-c;  
 
 if(R%2==0)
  cout<<"R is even= "<<R<<endl;
 else if (R%2!=0) 
  cout<<"R is odd"<<R<<endl;
  
 if( max < R ) max=R; 
 if( min > R ) min=R; 
    
 i++;
 
 cout<<i<<" a="<<a<<" b="<<b<<" c="<<c<<" a+b-c="<<a+b-c<<"  - n="<<n<<endl;
 
     if(i<n) goto E;
     
 cout<<" max= "<<max<<endl;
 cout<<" min= "<<min<<endl;
 
   return 0;
  }
you are making this way, way too hard. Do not use a goto unless you can't do the same thing with a loop or another tool. There are not many reasons to use goto in c++.

this looks like what happens when you code before thinking and designing the answer.
can you say, in words not code, exactly how you plan to solve this problem? If you cannot, then you sure can't code it!

first, you may not have an answer, so how will you handle that? The numbers are random or user input, either way, if the values are 3,5,7,9, what is the max and min even values? There are none, and you should say this. Same for odd, if all the numbers were even.

what happens if you do things as you get them?
in pseudo code.. consider:

bool ihazeven = false, ihazodd=false;
a = rand..
if (a%2 == 0)
{
maxeven = mineven = a;
ihazeven = true;
}
else
{
maxodd = minodd = a;
ihazodd = true;
}

b = rand...
... lots of logic for each one after a.
if(a and b are both even or odd and a > b) a is max{even or odd} and b is min{even or odd}
if(a and b are both even or odd and a < b){set a = min{e/o} b = max{e/o}
--update ihazeven/ihazodd if not both same as well

and c is the same except compare to both a and b,
and n is the same except even more compares, to a,b,c

however for c and n you already know how a relates to b, so if c is >a, and a > b, you know something, you don't need to check c > b, you know this.


@jonnin,
thank you so much for all the explanation provided, (bow)
@jonnin, check it out :
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
 int main()
 {
 	
     int a, b, c, n, i=0;
     int max=INT_MIN;    
     int min=INT_MAX;
     int maxp, minp;
     int maxi, maxp;
     
     
 cout<<"Enter n=";
 cin>>n;
 
 E: 
 
 a=rand()%10;
 b=rand()%10;
 c=rand()%10;
 
 int R=a+b-c;  
   
 if( maxp < R && R%2==0) maxp=R; 
 else if ( maxi < R && R%2!=0) maxp=R; 
 if( minp > R && R%2==0) minp=R;  
 else if(( mini > R && R%2!=0) mini=R;)
   
 i++;
 
 cout<<i<<" a="<<a<<" b="<<b<<" c="<<c<<" a+b-c="<<a+b-c<<"  - n="<<n<<endl;
     if(i<n)
	  goto E;
	  
 cout<<" max= "<<max<<endl;
 cout<<" min= "<<min<<endl;
 
 
   return 0;
  }
much better :)
goto is better avoided when a loop will do -- it is almost never used because it is difficult to follow in more complex problems.
Last edited on
Topic archived. No new replies allowed.