help me

can some one help me put these in ascending and descending 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
// Number Sorter
#include <iostream>


using namespace std;

char menu()
{
    char choice;
    cout<< "\nNumber Sorter - Created By Ben Tickle";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Ascending order ";
    cout<< "\n 2 - Descending order ";
    cout<< "\n 3 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return: ";
    cin >> choice;
    return choice;
}

int main()
{
    int a[5];
    
    
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nAscending order"
                     << "\n\n\nPlease enter 5 numbers include spaces:";
                
                cin >> a[0]; 
                cin >> a[1]; 
                cin >> a[2]; 
                cin >> a[3]; 
                cin >> a[4]; 

                for(int i=0;i<5;++i)
                cout<<a[i]<< ' '; 

                
                // Pause for user
                system("pause");
            break;             
            case '2':
                cout<< "\n\nDescending order"
                    << "\n\n\nPlease enter 5 numbers include spaces:";
                
                
                // Pause for user
                system("pause");
            break;
            case '3':
                cout<< "Exit"; 
                break;
            default:
                cout<< "\nNot a valid choice.";
        }
    } while (choice != '3');

    return 0;
} 
Well, simply use sorting, for example Bubble Sort - here it is described simply:

http://codeabbey.com/index/task_view/bubble-sort

You can also use built-in sorting function if your assignment allows this (I doubt it).

By the way for printing 5 numbers you can use "for" loop instead of printing them in separate line each.
can some run this and tell me why it wont compile?


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
#include <iostream>
using namespace std;
int main()
{
    int data[5]
    int i,j,temp,temp1;
    
    cout<<"Enter 5 Numbers\n";
    for(i=0;i<5;i++)
    {
    cin>>data[i];
    }
    temp=data[0];
    for(i=0;i,5;i++)
    {
    for(j=i;j<5;j++)
    {
    if(data[i] , data[j])
    {
    temp=data[i];
    data[i]=data[j];
    data[j]=temp;
    }
    }
    }
    cout<<"Descending Order\n";
    for(i=0;i,5;i++)
    {
    cout<<data[i]<<"\n";
    }
    return 0;
    }
You can use #include <algorithm> and use std::sort to sort things, but it is more convenient if you sort numbers, strings etc. when you input them into a vector. and in the previous code you entered when you type the variables in the for loops declare them as integers because they are being used in new scopes.
Topic archived. No new replies allowed.