Largest Number Performance

Ok, so I came across the problem (yes, it was class work but I have finished it and moved on 19 / 20 on it) and it required finding the largest number of 5 numbers entered. Now I ended up solving this uses if statements like this:
 
if(A > B && A > C && A > D && A > E) // Example statements would have 5 of these 

Now I was wondering if there is a better way of doing this, some include I could use or code I could write if I ever needed to solve something like this in the future.
Is there some way I could use an array and an include to sort it?
Or use a binary tree and always move backward?

Btw thanks for all of the help, you guys are great!
Last edited on
closed account (D80DSL3A)
Glad you got a good grade but I think there is an easier way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    cout << "Enter 5 integer values " << endl;
    int lowVal;
    cout << "value #1: "; 
    cin >> lowVal;;// 1st entry is low value
    
    // read in the remaining 4 values and compare to lowVal
    for( int i=2; i<=5; ++i )
    {
        int entry=0;
        cout << "value # " << i << ": "; 
        cin >> entry;// get next entry

        // here's the critical logic!
        if( entry < lowVal )
           lowVal = entry;// catch the new lowest yet value
    }
    // report result
    cout << "The lowest value entered was: " << lowVal << endl;

    return 0;
}
Cool, I never thought of this. With my solution if tow or more are tied for the lowest the if statements would output a 0 where as this would work. I had to use a series of booleans where if two or more are true then it presents a tie. That solution I made just isn't very practical for problems with more that 5ish inputs.
Topic archived. No new replies allowed.