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
|
#include <iostream>
using namespace std;
int main()
{
// create a 2 dimensional array, Ive gave it some
// values, saves clearning it out with a loop
int pancakes[2][10] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // row 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // row 1 (where qty will be)
};
int numPerson = 0, qtyPancakes = 0; // for sort
// lets read in 10 numbers
for (int i = 0; i < 10; i++) {
cout << "Enter number of pancakes for person " << i+1 << ": ";
cin >> pancakes[1][i]; // row 1 column i (refer to 2d array)
}
// sort them.
for (int i = 0; i<10; i++)
{
for (int j = 0; j<10; j++)
{
// check qty ate value
if (pancakes[1][i] > pancakes[1][j])
{
numPerson = pancakes[0][j];
qtyPancakes = pancakes[1][j];
pancakes[0][j] = pancakes[0][i];
pancakes[1][j] = pancakes[1][i];
pancakes[1][i] = qtyPancakes;
pancakes[0][i] = numPerson;
}
}
}
// display them
for (int i = 0; i < 10; i++)
cout << "Person " << pancakes[0][i] << " ate " << pancakes[1][i] << " pancakes." << endl;
return 0;
}
|
Enter number of pancakes for person 1: 55
Enter number of pancakes for person 2: 43
Enter number of pancakes for person 3: 36
Enter number of pancakes for person 4: 21
Enter number of pancakes for person 5: 77
Enter number of pancakes for person 6: 53
Enter number of pancakes for person 7: 67
Enter number of pancakes for person 8: 26
Enter number of pancakes for person 9: 10
Enter number of pancakes for person 10: 7
Person 5 ate 77 pancakes.
Person 7 ate 67 pancakes.
Person 1 ate 55 pancakes.
Person 6 ate 53 pancakes.
Person 2 ate 43 pancakes.
Person 3 ate 36 pancakes.
Person 8 ate 26 pancakes.
Person 4 ate 21 pancakes.
Person 9 ate 10 pancakes.
Person 10 ate 7 pancakes.
|