Display 3 numbers from highest to lowest, vice versa

Aug 28, 2014 at 11:15am
So I need to write a program that can display three numbers from highest to lowest, and then from lowest to highest. My professor told us we could find out how to do it by reading our current lesson, but the method I found only works for two numbers. Anyways, here's what I ended up with after some experimenting.

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
#include <iostream>
using namespace std;
int main()
{
    system("CLS");
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    
    cout<<"Enter first number: ";
    cin>>num1;
    
    cout<<"Enter second number: ";
    cin>>num2;
    
    cout<<"Enter third number: ";
    cin>>num3;
    
    if (num1 > num2)
    {
        cout<<"Highest: "<<num1;
        cout<<"Middle: "<<num2;
        cout<<"Lowest: "<<num3;
        cout<<endl<<endl;
    }
    
    else if (num1 > num3)
    {
        cout<<"Highest: "<<num1;
        cout<<"Middle: "<<num3;
        cout<<"Lowest: "<<num2;
        cout<<endl<<endl;
    }
    
    else if (num2 > num1)
    {
        cout<<"Highest: "<<num2;
        cout<<"Middle: "<<num1;
        cout<<"Lowest: "<<num3;
        cout<<endl<<endl;
    }
    
    else if (num2 > num3)
    {
        cout<<"Highest: "<<num2;
        cout<<"Middle: "<<num3;
        cout<<"Lowest: "<<num1;
        cout<<endl<<endl;
    }
    
    
    else if (num3 > num2)
    {
        cout<<"Highest: "<<num3;
        cout<<"Middle: "<<num2;
        cout<<"Lowest: "<<num1;
        cout<<endl<<endl;
    }
    
    else if (num3 > num1)
    {
        cout<<"Highest: "<<num3;
        cout<<"Middle: "<<num1;
        cout<<"Lowest: "<<num2;
        cout<<endl<<endl;
    }
    system("PAUSE");
}


It works for the first part, but it doesn't work for any of the others. I did some searching and found out about something called bubble sort, but I think that's a bit too ahead.
Aug 28, 2014 at 12:52pm
Since there are three variables, you'll have to do the check with all three.
Eg:
1
2
if(num1>num2 && num1>num3)
     cout<<"highest is: "<<num1;


Aceix.
Topic archived. No new replies allowed.