Help with arranging numbers in descending order.

Hey every in new to programming and need to help creating a program that arranges 3 numbers in descending order. I have searched online for help, but many of the coding is too advance for me to understand. Any help would be welcome.



#include <iostream>
using namespace std;


void descend_order(int num1, int num2, int num3);
int num1,num2,num3;


int main ()
{
int num1,num2,num3;

cout<<"Enter three numbers:";
cin>> num1>>num2>>num3;

descend_order(num1, num2, num3);
cout<<"Descending order:"<<descend_order<<endl;
return 0;
}

void descend_order (int num1, int num2, int num3)
{


if (num1 >= num2 && num2 >= num3)
{
cout<<num1<<num2<<num3<<endl;
//123
}

else if (num1 >= num3 && num3 >= num2)
{
cout<< num1<<num3<<num2<<endl;
//132
}

else if (num2 >= num1 && num1 >= num3)
{
cout<< num2<<num1<<num3<<endl;
//213
}

else if (num2 >= num3 && num3 >= num1)
{
cout<< num2<< num3<<num1<<endl;
//231
}

else if (num3 >= num1 && num1 >= num2)
{
cout<<num3<<num1<<num2<<endl;
//312
}

else if (num3 >= num2 && num2 >= num1)
{
cout<<num3<<num2<<num1<<endl;
//321
}
return descend_order(num1,num2,num3);

}
First off all please use code tags and indent properly, something like this:
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
#include <iostream>
using namespace std;


void descend_order(int num1, int num2, int num3);
int num1,num2,num3;


int main ()
{
    int num1,num2,num3;

    cout<<"Enter three numbers:";
    cin>> num1>>num2>>num3;

    descend_order(num1, num2, num3);
    cout<<"Descending order:"<<descend_order<<endl;
    return 0;
}

void descend_order (int num1, int num2, int num3)
{


    if (num1 >= num2 && num2 >= num3) 
    {
        cout<<num1<<num2<<num3<<endl;
        //123
    }

    else if (num1 >= num3 && num3 >= num2)
    {
        cout<< num1<<num3<<num2<<endl;
        //132
    }

    else if (num2 >= num1 && num1 >= num3)
    {
        cout<< num2<<num1<<num3<<endl;
        //213
    }

    else if (num2 >= num3 && num3 >= num1)
    {
        cout<< num2<< num3<<num1<<endl;
        //231
    }

    else if (num3 >= num1 && num1 >= num2)
    {
        cout<<num3<<num1<<num2<<endl; 
        //312
    }

    else if (num3 >= num2 && num2 >= num1)
    {
        cout<<num3<<num2<<num1<<endl;
        //321
    }
    return descend_order(num1,num2,num3);

}


I guessing your problem is that it never finishes(?). That's because you are returning a call to the function so it start recursion.

Erase that return, void function don't need it or replace with return;

Edit: Just double check and some more stuff:
No need to double declare num1,num2,num3, just inside of main is fine.
This:
1
2
descend_order(num1, num2, num3);
cout<<"Descending order:"<<descend_order<<endl;

Should become:
cout<<"Descending order:"<<descend_order(num1, num2, num3)<<endl;
1
2
cout<<"Descending order:";
descend_order(num1,num2,num3);
Last edited on
This is a classic sort problem.

Don't think about the numbers individually. It makes it difficult to extend your function if you want to sort 4, 5 or <N> numbers.

Instead think about putting the numbers in array, then looping through the array tranposing any two adjacent numbers where the first number is less than the second number of the pair. This makes it easy to extend your solution to sort any number of numbers.

PLEASE USE CODE TAGS (the <> formatting button when posting code. It makes it easier to read and respond to your post.
Thanks guys for all your help this is my code so far with all of the corrections suggested:
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
 #include <iostream>
using namespace std;


void descend_order(int num1, int num2, int num3);
int num1,num2,num3;


int main ()
{
    
	
    cout<<"Enter three numbers:";
    cin>> num1>>num2>>num3;

    descend_order(num1,num2,num3);
	cout<<"Descending order:"<<descend_order(num1,num2,num3)<<endl;
    return 0;
}

void descend_order (int num1, int num2, int num3)
{


    if (num1 >= num2 && num2 >= num3) 
    {
        cout<<num1<<num2<<num3<<endl;
        //123
    }

    else if (num1 >= num3 && num3 >= num2)
    {
        cout<< num1<<num3<<num2<<endl;
        //132
    }

    else if (num2 >= num1 && num1 >= num3)
    {
        cout<< num2<<num1<<num3<<endl;
        //213
    }

    else if (num2 >= num3 && num3 >= num1)
    {
        cout<< num2<< num3<<num1<<endl;
        //231
    }

    else if (num3 >= num1 && num1 >= num2)
    {
        cout<<num3<<num1<<num2<<endl; 
        //312
    }

    else if (num3 >= num2 && num2 >= num1)
    {
        cout<<num3<<num2<<num1<<endl;
        //321
    }
    return ;

}  




Im am still getting an error though.
error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘void’)
  cout<<"Descending order:"<<descend_order(num1,num2,num3)<<endl;


cout<<"Descending order:"<<descend_order(num1,num2,num3)<<endl;

descend_order is a void function - it doesn't return a value for cout to output.
Last edited on
That is the same error I am getting.
Yeah my bad(already edited the comment) the line exchane line 16 and 17 and line 17 becomes cout<<"Descending order:";

Remember that descend_order(...) doesn't return anything so it can't be used that way.
Last edited on
something like this:
1
2
3
4
5
6
7
 cout<<"Enter three numbers:";
 cin>> num1>>num2>>num3;

    
  cout<<"Descending order:";
  descend_order(num1,num2,num3);
    return 0;
I finally figured it out. I want to thank everyone that helped me out. I will post the code for future reference to anyone that wants it. I know this is the worst way to do this, but for know this is the only way I know how to do it.
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
 #include <iostream>
using namespace std;


void descend_order(int num1, int num2, int num3);
int num1,num2,num3;


int main ()
{
    
	
    cout<<"Enter three numbers:";
    cin>> num1>>num2>>num3;

    
	cout<<"Descending order:";
	descend_order(num1,num2,num3);
    return 0;
}

void descend_order (int num1, int num2, int num3)
{


    if (num1 >= num2 && num2 >= num3) 
    {
        cout<<num1 <<" "<< num2<<" " << num3<<endl;
        //123
    }

    else if (num1 >= num3 && num3 >= num2)
    {
        cout<< num1<<" "<<num3<<" "<<num2<<endl;
        //132
    }

    else if (num2 >= num1 && num1 >= num3)
    {
        cout<< num2<<" "<<num1<<" "<<num3<<endl;
        //213
    }

    else if (num2 >= num3 && num3 >= num1)
    {
        cout<< num2<<" "<< num3<<" "<<num1<<endl;
        //231
    }

    else if (num3 >= num1 && num1 >= num2)
    {
        cout<<num3<<" "<<num1<<" "<<num2<<endl; 
        //312
    }

    else if (num3 >= num2 && num2 >= num1)
    {
        cout<<num3<<" "<<num2<<" "<<num1<<endl;
        //321
    }
    return  ;

}
Now extend your code to sort 4 numbers in descending order.
Topic archived. No new replies allowed.