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
|
#include<iostream>
#include<iomanip>
using namespace std;
// Function sortem()
void sortem(int array[][3], int col, int start, int stop)
{
int row, // The row I'm in
smallestSubscript, // Smallest subscript found when sorting through the loop
smallestValue, // Smallest value found in that subscript
smallestSubscript2, // Same as above, used for payroll
smallestValue2; // Same as above, used for payroll
// I took the easy way out
// If you're in the department column, sort in ascending order
if (col == 0)
{
for(row = start; row < (stop - 1); row++)
{
smallestSubscript = row;
smallestValue = array[row][0];
for (int index = row + 1; index < stop; index++)
{
if (array[index][0] < smallestValue)
{
smallestValue = array[index][0];
smallestSubscript = index;
}
}
array[smallestSubscript][0] = array[row][0];
array[row][0] = smallestValue;
}
}
/* If you're in the employee column, sort in ascending order (the main function takes care of sorting within the department),
and also swap out the payroll in column 3 */
if (col == 1)
{
for(row = start; row < (stop - 1); row++)
{
smallestSubscript = row;
smallestSubscript2 = row;
smallestValue = array[row][1];
smallestValue2 = array[row][2];
for (int index = row + 1; index < stop; index++)
{
if (array[index][1] < smallestValue)
{
smallestValue = array[index][1];
smallestValue2 = array[index][2];
smallestSubscript = index;
smallestSubscript2 = index;
}
}
array[smallestSubscript][1] = array[row][1];
array[smallestSubscript2][2] = array[row][2];
array[row][1] = smallestValue;
array[row][2] = smallestValue2;
}
}
}
// Main function
int main()
{
int keyColumn, rowStart, rowEnd;
// departement, employee, and payroll data
int array[16][3] = {{2, 5, 1050},
{5, 17, 1200},
{3, 12, 1225},
{2, 23, 1300},
{3, 6, 1140},
{3, 3, 1110},
{2, 4, 1420},
{2, 1, 1360},
{5, 14, 1225},
{5, 11, 1380},
{5, 10, 1410},
{2, 2, 1565},
{3, 8, 1445},
{5, 7, 1387},
{3, 9, 1128},
{2, 16, 1488}};
//print array
cout << "The unsorted array\n\n";
cout << right << setw(11) << "Department" << setw(13) << "Employee" << setw(16) << "Monthly Pay\n";
cout << setw(11) << "----------" << setw(13) << "--------" << setw(16) << "-----------\n";
for(int i = 0; i < 16; i++)
{
cout << left << " " << setw(15) << array[i][0] << setw(12) << array[i][1] << setw(15) << array[i][2] << endl;
}
cout << endl;
system("pause");
cout << "\n\n";
// Sort the departments into ascending order.
keyColumn = 0; // This is the column you are in
rowStart = 0; // What row to start with
rowEnd = 16; // What row to end with
sortem(array, keyColumn, rowStart, rowEnd);
// Sort employees into ascending order
rowStart = 0;
keyColumn = 1;
do
{
rowEnd = rowStart; // find last row for this dept. number
while( array[rowEnd][0] == array[rowStart][0] )
rowEnd++;
//Now sort on employee number for this department
sortem(array, keyColumn, rowStart, rowEnd);
rowStart = rowEnd;
} while( array[rowStart][0] >= 0 );//keep going til last record
//Finally, print the sorted table
cout << "The array sorted via a selection sort\n\n";
cout << right << setw(11) << "Department" << setw(13) << "Employee" << setw(16) << "Monthly Pay\n";
cout << setw(11) << "----------" << setw(13) << "--------" << setw(16) << "-----------\n";
for(int i = 0; i < 16; i++)
{
cout << left << " " << setw(15) << array[i][0] << setw(12) << array[i][1] << setw(15) << array[i][2] << endl;
}
cout << endl;
system("pause");
}
|