Greatest to least problem

What is wrong with it? It is not outputting in the correct order.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream> // library for input/output
#include <conio.h> // console input/output

using namespace std;

int FindGreatest(int, int, int, int);
int FindMiddle(int, int, int, int);
int FindLeast(int, int, int, int);

int main()
{
    int number_one;
    int number_two;
    int number_three;
    int largest;
    int middle;
    int least;

    cout << "Enter the first number: " << endl;
    cin >> number_one;

    cout << "Enter the second number: " << endl;
    cin >> number_two;

    cout << "Enter the third number: " << endl;
    cin >> number_three;

    largest = FindGreatest(number_one, number_two, number_three, largest);
    middle = FindMiddle(number_one, number_two, number_three, middle);
    least = FindLeast(number_one, number_two, number_three, least);

    cout << "\n" << largest << " " << middle << " " << least << endl;

    _getch();
    return 0;
}
int FindGreatest(int number_one, int number_two, int number_three, int largest)
{
    if( number_one > number_two)
        largest = number_one;
    else if( number_two > number_one)
        largest = number_two;
    else if( number_three > number_one && number_three > number_two )
        largest = number_three;

    return largest;
}
int FindMiddle(int number_one, int number_two, int number_three, int middle)
{
    int least;
    int largest;

    if( number_one < number_two)
        least = number_one;
    else if( number_two < number_one)
        least = number_two;
    else if( number_three < number_one && number_three < number_two)
        least = number_three;

     if( number_one > number_two)
        largest = number_one;
    else if( number_two > number_one)
        largest = number_two;
     else if( number_three > number_one && number_three > number_two )
        largest = number_three;

    middle = ( number_one + number_two + number_three) - (largest + least);

    return middle;
}
int FindLeast(int number_one, int number_two, int number_three, int least)
{
    if( number_one < number_two)
        least = number_one;
    else if( number_two < number_one)
        least = number_two;
    else if( number_three < number_one && number_three < number_two)
        least = number_three;

    return least;
}
Your function FindGreatest doesn't work.

What happens if the three numbers are:

number_one = 7;
number_two = 5;
number_three = 1004;

Which comes out as largest? number_one. That's clearly wrong. You need to slowly go through that code yourself with those inputs, following the logic, until you see where it doesn't work. Either in your head or on paper.

Similarly for your other functions. Your logic of how to find the value you're looking for is just all wrong.
Last edited on
In functions you have tested only one condition with if.
Lets say numbers are 6,5,7 then first condition satisfies and largest=6...
Similar errors in others functions also.


Also you can reduce program by changing return type to void and pass largest and middle and least as reference.

Corrected and shorted program:
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
#include <iostream.h> // library for input/output
#include <conio.h> // console input/output

void FindGreatest(int, int, int, int&);
void FindMiddle(int, int, int, int&);
void FindLeast(int, int, int, int&);

int main()
{
    clrscr();
    int number_one;
    int number_two;
    int number_three;
    int largest;
    int middle;
    int least;

    cout << "Enter the first number: " << endl;
    cin >> number_one;

    cout << "Enter the second number: " << endl;
    cin >> number_two;

    cout << "Enter the third number: " << endl;
    cin >> number_three;

    FindGreatest(number_one, number_two, number_three, largest);
    FindMiddle(number_one, number_two, number_three, middle);
    FindLeast(number_one, number_two, number_three, least);

    cout << "\n" << largest << " " << middle << " " << least << endl;

    getch();
    return 0;
}
void FindGreatest(int number_one, int number_two, int number_three, int &largest)
{
    if( number_one > number_two && number_one > number_three)
	largest = number_one;
    else if( number_two > number_one && number_two > number_three)
	largest = number_two;
    else
	largest = number_three;

}
void FindMiddle(int number_one, int number_two, int number_three, int &middle)
{   //doing what you wanted to do in short
    int l_least;
    int l_largest;

    FindGreatest(number_one, number_two, number_three, l_largest);
    FindLeast(number_one, number_two, number_three, l_least);

    middle = ( number_one + number_two + number_three) - (l_largest + l_least);

}
void FindLeast(int number_one, int number_two, int number_three, int &least)
{
    if( number_one < number_two && number_one < number_three)
	least = number_one;
    else if( number_two < number_one && number_two < number_three)
	least = number_two;
    else
	least = number_three;
}
Thanks for the help from both of you!

Moschops, thanks for saying to go back and work it out.

I went back and re-wrote my program and had it almost working. I used else if instead of a plain else. Didn't notice it until Akshit posted. Other than that my re-written program was correct.
Last edited on
Topic archived. No new replies allowed.