sum of array indecies

I have to write a program that consists of the following:
- Function to add up 5 salaries (I should use arrays)
- Function to find the maximum salary
- Function to sort the array in descending order
- Function to sum up the salaries
- Function to display the salaries in tabular format
- Function to search for a specific salary
- Function to sum up the last 3 salaries

Here is my code
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <iomanip>
using namespace std;

/*functions prototypes start*/
void ImportSalaries(int [], int);
int SearchSalaries(int [], int, int);
int MaximumSalary(int [], int);
void SortSalaries(int [], int);
void DisplaySalaries(int [], int);
int AddSalaries(int [], int);
void seperator();
/*functions prototypes end*/

int main() {
    const int arraySize = 5;
    int salaries[arraySize];
    char choices;
    ImportSalaries(salaries, arraySize);
    while ((choices = cin.get()) != EOF) {
        cout << "(a) Find the maximum salary\n";
        cout << "(b) Search for the position number of a given salary\n";
        cout << "(c) Sort salaries in descending order\n";
        cout << "(d) Display salaries\n";
        cout << "(e) Display salaries sum\n";
        cout << "(f) Display the sum of the last three salaries\n";
        seperator();
        cout << "\nChoice: ";
        cin >> choices;
        switch(choices) {
            case 'a':
                cout << "\nMaximum salary is: " << MaximumSalary(salaries, arraySize) << endl;
                seperator();
                cout << "\n\n";
                break;
            case 'b':
                int salary, salaryPostion;
                cout << "Enter salary to lookup it's postion: ";
                cin >> salary;
                salaryPostion = SearchSalaries(salaries, arraySize, salary);
                if (salaryPostion != -1)
                    cout << "\nSalary position is: " << salaryPostion << endl;
                else
                    cout << "\nSalary not found!\n";
                seperator();
                cout << "\n\n";
                break;
            case 'c':
                SortSalaries(salaries, arraySize);
                cout << "\nSorted salaries: ";
                for (int num = 0; num < arraySize; num++)
                    cout << salaries[num] << ' ';
                cout << endl;
                seperator();
                cout << "\n\n";
                break;
            case 'd':
                cout << '\n';
                DisplaySalaries(salaries, arraySize);
                seperator();
                cout << "\n\n";
                break;
            case 'e':
                cout << "\nSalary sum: " << AddSalaries(salaries, arraySize);
                cout << endl;
                seperator();
                cout << "\n\n";
                break;
            case 'f':
                cout << "\nLast three salaries sum: " << AddSalaries(salaries, arraySize) - (salaries[0] + salaries[1]);
                cout << endl;
                seperator();
                cout << "\n\n";
                break;
            default:
                cout <<"Invalid option\n";
        }
    }
}

void ImportSalaries(int x[], int y) {
    for (int i = 0; i < y; i++) {
        cout << "Enter a salary: ";
        cin >> x[i];
    }
}

int SearchSalaries(int x[], int y, int keywordValue) {
    for (int i = 0; i < y; i++) //loop through array values
        if (x[i] == keywordValue) //if keyword value is equal to the current array value
            return i; //value is found
        return -1; //value not found
}

int MaximumSalary(int x[], int y) {
    int max = x[0];
    for (int i = 1; i < y; i++)
        if (x[i] > max)
            max = x[i];
    return max;
}

void SortSalaries(int x[], int y) {
    int last = y-2, temp;
    int isChanged = 1;
    while (last >= 0 && isChanged) {
        isChanged = 0;
        for (int k = 0; k <= last; k++)
            if (x[k] > x[k+1]) {
                temp = x[k];
                x[k] = x[k+1];
                x[k+1] = temp;
                isChanged = 1;
            }
        last--;
    }
}

void DisplaySalaries(int x[], int y) {
    cout << "Employee Number" << setw(10) << "Salary\n";
    for (int i = 0; i < y; i++)
        cout << i+1 << setw(20) << x[i] << '\n';
}

int AddSalaries(int x[], int y) {
    int salariesSum = 0;
    for (int i = 0; i < y; i++)
        salariesSum += x[i];
    return salariesSum;
}

void seperator() {
    for (int i = 1; i <= 55; i++)
            cout << '_';
}


My Dr. said that I shouldn't create a new function to sum up the last three arrays and I should NOT subtract the first two values from the salaries total. What should I do then? Is there any other way? She said there is a trick, but i can't get a hold of it. Any help would be highly appreciated.

Thanks folks
Last edited on
First of all I would like to say that I am very pleased with the formatting of your Program. It is very well typed and legible.

My Dr. said that I shouldn't create a new function to sum up the last three arrays

You didnt. You have a function that sums the all of the salaries so you are good to go here

I should NOT subtract the first two values from the salaries total

Shes correct. The reason you shouldnt do this is because if the array size were to change to something different your result wouldnt be correct. Say for example you changed the array size to 15. Your sum of the last three would now include 12 salaries not the last 3. So i think what she is saying is to make your program work for any size array. As far as a trick for doing it, I dont know what "Trick" she is referring to.
If you are not supposed to create a function to do this. You can do it like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
case 'f':  
               if(arraySize <= 3)
               {
                     cout << "\nLast three salaries sum: " << AddSalaries(salaries, arraySize)<<endl;
               }
               else
               {
                     cout << "\nLast three salaries sum: " << AddSalaries(salaries, arraySize) - AddSalaries(salaries, arraySize - 3);
                }
                seperator();
                cout << "\n\n";
                break;


Hope this is the "Trick" But this should work if you were to change the size of the array at the top of the program
AddSalaries(salaries, arraySize) - AddSalaries(salaries, arraySize - 3);

That's a little roundabout, isn't it?

Why not:

AddSalaries(salaries + arraySize - 3, 3);


Although I guess if you didn't learn about pointer math yet....
When you refer to pointer math are you talking about something like this
array[5] == *(array + 5)...

It slipped my mind to do it that way. But yea ive never really run into issues where i had to use that. I know it is possible. Thanks for that reply.

I also know that when you pass an array to a function it actually points at the first positon in the array and I never even thought about changing that position in order to add up the last three. That is great info to know though and is exactly why i recommend my classmates to this site because of people like you.
We are not allowed to use pointers in this assignment, but thanks for the explanation =)

Thanks again guys your advices saved my ass
Last edited on
Topic archived. No new replies allowed.