trying to find largest int

user inputs 3 integers
instructor wants us to assume first int is lowest, and find the highest int and print it out. using two if statements.
my code is just spitting out last int
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
 #include <iostream>
#include <cstring>
using namespace std;
int
main()
{
   int a;
   int b;
   int c;
   int smallest;
   int largest;
    cout << "\nInput integer values for a, b, and c: ";
    cin >> a >> b >> c;
    if ( a>0,b>0,c>0) {

     {

     } largest == a;
      if (b > largest && b>c) {
            largest = b;}

            if (c > a && c > largest );
               { largest = c;
       }

        cout <<"Largest is " << largest << endl;
           }

           else {
       cout << "Sorry, all numbers must be larger than zero.  Please\n";
       cout << "try again." << endl;
   }
return 0 ;
}
Last edited on
Why lines 14 and 30? You said "3 integers", not "3 positive non-zero integers".

Line 11: unused variable.

Lines 16-18 make no sense. An assignment would make sense.

Finding a maximum from an array:
1
2
3
4
5
6
winner = arr[0];
size_t i = 1;
while ( i < n ) {
  if ( winner < arr[i] ) winner = arr[i];
  ++i;
}

Compare that to your logic. [Edit: fixed the relational operator]
Last edited on
sorry three integers greater then zero. again I have explicit directions this is how the instructor wants it. No array's I have a logic error somewhere. Been studying for my midterms and my eyes are missing something small I'm sure
if ( a>0,b>0,c>0) {
Why line 14 indeed.
The comma operator (represented by the token ',') evaluates a sequence of expressions, and returns the left-most.

With the line you've written there, you're not checking if all those values are larger than zero. Effectively, you're only checking if c is larger than zero.
Last edited on
I said "compare", not "use". Pretend that n==3. Unroll the loop. Replace array elements with your variables appropriately. What do you get?
thanks xism working on 4 hrs sleep . changed (,) to (&&)
Last edited on
Here if anyone is interested. away to find largest int out of three. Had some logical errors I corrected. Finally got time to post the fix. Some may be skeptical about why this was done the way it was. It was because I was following my instructors directions.
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
 #include <iostream>
#include <cstring>
using namespace std;
int
main()
{
   int a;
   int b;
   int c;
   int smallest;
   int largest;
    cout << "\nInput integer values for a, b, and c: ";
    cin >> a >> b >> c;
    if ( a>0,b>0,c>0){



     {

      largest = a;
      if (b > largest && b>c)
            largest = b;

            if (c > a && c > largest )
                largest = c;
     }


        cout <<"Largest is " << largest << endl;}

           else
       {cout << "Sorry, all numbers must be larger than zero.  Please\n";
       cout << "try again." << endl;
   }
return 0 ;
}
Line 14...
also your logic seems a bit off.

you are setting largest to a.

Then on line 21

If b is larger than a(largest) and b > c
largest = b

then on line 24 you check if c is greater than a(why?) and largest

Heres what I would do.

set largest equal to a
check if b is larger than largest (a) if so assign b to largest.
check if c is larger than largest(a or b) if so assign c to largest

1
2
3
4
5
largest = a;
if( b > largest )
    largest = b;
if( c > largest )
    largest = c;
He said that he has fixed that. If so, then there is version control issue, i.e. he still has "bad" versions around to copy-paste from.

Line 12 probably should tell the user that only positive natural numbers (N) are expected, rather than the whole mathematical set of integers (Z). The current version is like going to exam and having to write the answers before the questions are revealed.

On should include line 13 in line 14. if ( cin >> a >> b >> c && 0 < a && 0 < b && ...
The reason is that the user can type silly things and then a, b, and/or c might have undefined value. Obviously, the ELSE branch has to check whether the IF failed due to input error or due to non-positive input.

"Unroll loop", for fun:
1
2
3
4
5
6
winner = arr[0];
size_t i = 1;
while ( i < n ) {
  if ( winner < arr[i] ) winner = arr[i];
  ++i;
}

Set n = 3. We can now replace the while with repeating instructions:
1
2
3
4
5
6
winner = arr[0];
size_t i = 1;
if ( winner < arr[i] ) winner = arr[i];
++i;
if ( winner < arr[i] ) winner = arr[i];
++i;
winner == arr[0]
i == 1 < n
arr[i] == arr[1]
i == 2 < n
arr[i] == arr[2]
i == 3 == n

And remove the i altogether:
1
2
3
winner = arr[0];
if ( winner < arr[1] ) winner = arr[1];
if ( winner < arr[2] ) winner = arr[2];

Replacing arr[0], arr[1], and arr[2] with a, b, and c is now trivial.
Last edited on
Topic archived. No new replies allowed.