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 << '_';
}
|