help

can someone show me how to change this into arrays

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
// Maths Helper
#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 a1,a2,a3,a4,a5;
    
    
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nAscending order"
                     << "\n\n\nPlease enter 5 numbers include spaces:";
                
                
cin >> a1; 
cin >> a2; 
cin >> a3; 
cin >> a4; 
cin >> a5; 
for(int i=0;i<4;++i) 
{ 
if(a1>a2){ int tmp=a1; a1=a2; a2=tmp; } 
if(a2>a3){ int tmp=a2; a2=a3; a3=tmp; } 
if(a3>a4){ int tmp=a3; a3=a4; a4=tmp; } 
if(a4>a5){ int tmp=a4; a4=a5; a5=tmp; } 
} 
cout << "1st: " << a1 << endl; 
cout << "2nd: " << a2 << endl; 
cout << "3rd: " << a3 << endl; 
cout << "4th: " << a4 << endl; 
cout << "5th: " << a5 << endl; 
                
                // 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;
} 
Do you want to use an array to store the numbers? If yes:

1
2
3
4
5
6
7
8
9
10
11
12
    int a1,a2,a3,a4,a5;

// becomes

    int a[5];

// and because counting in arrays starts from zero:
// a1 == a[0]
// a2 == a[1]
// a3 == a[2]
// a4 == a[3]
// a5 == a[4] 


You already know how to use for() loops.
So to print an array of five elements you would write:

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

Last edited on
is this what you were saying?

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
// Maths Helper
#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 a1,a2,a3,a4,a5;
    
    
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nAscending order"
                     << "\n\n\nPlease enter 5 numbers include spaces:";
                int a[5];
                

                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;
} 
Last edited on
I am trying to create a code which will generate a payslip.

There are three categories
Category A are paid on hourly rate of 100 sh and a basic pay of 10000 sh
Category B are paid an hourly rate of 200 sh

Category C get 300 per hour.
The money is taxed as below:
First 10000-0%
Upto 20000-5 %
Upto 30,000-10 %
Otherwise at 20 %
Please assist
I am trying to create a code which will generate a payslip.

Please don't hijack other users' threads. Start your own.
Topic archived. No new replies allowed.