Ordering Numbers Highest, Medium and Lowest

Hi there Im trying to work out when the user enters three numbers how to get them to be ordered from Low, medium and high as an output message but this code wont compile.

cout << "Please Enter The first integer: ";
cin >> numb1;

cout << "Please Enter The second integer: ";
cin >> numb2;

cout << "Please Enter The third integer: ";
cin >> numb3;

cout << "Highest: "<< larger(numb1, numb2, numb3)<< endl;
cout << "Lowest: " << lowest(numb1, numb2, numb3) << endl;
cout << "Medium: " << medium(numb1, numb2, numb3) << endl;
Last edited on
You should define functions larger, lowest and medium.
How do you do that?
Are you allowed to use standard algorithms? For example the highest and lowest can be obtained by using std::max and std::min

cout << "Highest: "<< std::max( { numb1, numb2, numb3 } )<< endl;
cout << "Lowest: " << std::min( { numb1, numb2, numb3 } ) << endl;
Or you can use even std::sort for std::initializer_list<int>.:)
Oh, I am wrong. Iterators of std::initializer_list are constant.
Last edited on
thats brilliant thanks! do i have to use an <include> for this
You should include <algorithm>. But I think that the assignment is to write the functions yourself.
does #include <stdio.h> work, sorry for all the questions, im a begginner?
I think you should simply sort these three numbers. Then there will not any difficulty fo define the largest, smallest and medium elements.

An example of sorting


int a = 3, b = 2, c = 1;

std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;

if ( b < a ) std::swap( a, b );
if ( c < b ) std::swap( b, c );
if ( b < a ) std::swap( a, b );

std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
@cedwards

does #include <stdio.h> work, sorry for all the questions, im a begginner?


You have to use #include <iostream>. There is no any need to use <stdio.h>
ahhh brilliant! shall i include #include <algorithm>
also do you know how to do the medium? im guessing its

 
cout << "Medium: " << std::med( { numb1, numb2, numb3 } ) << endl;
closed account (28poGNh0)
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
# include <iostream>
using namespace std;

int get_max(int a,int b,int c)
{
    int tmpVar = (b>c)?b:c;
    return (a>tmpVar)?a:tmpVar;
}

int get_min(int a,int b,int c)
{
    int tmpVar = (b<c)?b:c;
    return (a<tmpVar)?a:tmpVar;
}

int get_mid(int a,int b,int c)
{
    if(a==b&&a==c)
    return a;

    if(a==b||a==c||b==c)
    {
        if(a==b)
            return a;
        if(a==c)
            return a;
        if(b==c)
            return b;
    }

    if((a>b&&a<c)||(a<b&&a>c))
        return a;
    else if((b>a&&b<c)||(b<a&&b>c))
        return b;
    else return c;
}

int main()
{
    int num1,num2,num3;
    int Max,Min,Mid;

    cout << "Please,Enter three numbers" << endl;
    cin >> num1 >> num2 >> num3;

    Max = get_max(num1,num2,num3);
    Min = get_min(num1,num2,num3);

    cout << "The max : " << Max << endl;
    cout << "The min : " << Min << endl;

    // For me with num1,num2,num3 ,middle function could be compilcated because we
    // could have two numbers or three equal

    Mid = get_mid(num1,num2,num3);
    cout << "The mid : " << Mid << endl;

    return 0;
}


I hope that helps
Topic archived. No new replies allowed.