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
|
#include <iostream>
#include <iomanip>
using namespace std;
void SelectionSort(string [], int pancakes[], int);
const int people = 10;
int main()
{
int pancakes[people];
string person[people] = {"Jamie", "Jordan", "Peele", "Ash", "Luis",
"Jimmy", "Kurt", "Kaiser", "Anthony", "Ryan"};
for(int i=0; i<10; i++)
{
cout<< "How many pancakes did " << person[i] << " eat? ";
cin >> pancakes[i];
}
cout << "\n" << "Names " << "\t";
cout << " Total pancakes eaten" << endl;
cout << "-----------------------------------";
SelectionSort(person, pancakes, people);
return 0;
}
void SelectionSort(string person[], int pancakes[], int SIZE)
{
int i;
int j;
int max;
for (i = 0; i < SIZE - 1; i++)
{
max = i; // The intial subscript or the first element.
for (j = i + 1; j < SIZE; j++)
{
if (pancakes[j] > pancakes[max]) /* If this element is greater,
then it is the new maximum */
{
max = j;
}
}
// Swap both variables at the same time.
int temp = pancakes[i];
pancakes[i] = pancakes[max];
pancakes[max] = temp;
string tempString = person[i];
person[i] = person[max];
person[max] = tempString;
}
for(i=0; i<10; i++)
{
cout << "\n" << left << setw(9) << person[i] << "\t";
cout << pancakes[i] << endl;
}
cout << "-----------------------------------";
cout << "\n\n" << person[0] << " eat the most pancakes. " << endl;
cout << person[9] << " eat the least pancakes. " << endl;
cout << pancakes[0] << " was the largest amount. " << endl;
cout << pancakes[9] << " was the smallest amount. " << endl;
}
|