changing from ascending to descending

can someone help me on why this 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
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
82
83
84
85
86
87
88
// 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]; 

                void BubbleSort(apvector <int> &num)
{
      int i, j, flag = 1;    // set flag to 1 to start first pass
      int temp;             // holding variable
      int numLength = num.length( ); 
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (num[j+1] > num[j])      // ascending order simply changes to <
              { 
                    temp = num[j];             // swap elements
                    num[j] = num[j+1];
                    num[j+1] = temp;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned
}

                
                // 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;
} 
Last edited on
what are the errors? "This wont compile" is useless.
Hi,

Try to fix this errors in the code:

1. Move the function Bubble sort out of the main
2. I don't know what kind of parameters you want to use in Bubble sort but I guess is just a vector<int> &num
3. There is no such length property in vectors, you can use size
4. You are not including libraries

When you compile, any IDE gives you an output of the errors, there you can find why is not compiling, use that tool to correct your code ;)

Best regards
the only one i cant figure out is the numlength on line 49 it says

`num' undeclared (first use this function)
Last edited on
Because you have not declared anything called 'num' perhaps???
could some one tell me how i could change my descending from ascending to descending?
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Number Sorter
#include <iostream>

#include <algorithm> 
#include <functional> 


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];
    int b[5];
    int const size = 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]; 
{ 
    
    

    std::sort(a, a+size, std::greater<int>()); 

    for(int i = 0; i<size; ++i) 
        std::cout << a[i] << " "; 
    std::cout << std::endl; 
    
} 
                // Pause for user
                system("pause");
            break;             
            case '2':
                cout<< "\n\nDescending order"
                    << "\n\n\nPlease enter 5 numbers include spaces:";
                    
                cin >> b[0]; 
                cin >> b[1]; 
                cin >> b[2]; 
                cin >> b[3]; 
                cin >> b[4];
{ 
    
    

    std::sort(b, b+size, std::greater<int>()); 

    for(int i = 0; i<size; ++i) 
        std::cout << b[i] << " "; 
    std::cout << std::endl; 
    
}
                
                
                // Pause for user
                system("pause");
            break;
            case '3':
                cout<< "Exit"; 
                break;
            default:
                cout<< "\nNot a valid choice.";
        }
    } while (choice != '3');

    return 0;
} 
Topic archived. No new replies allowed.