This is what I've written for the third part of the pancake glutton program.
I have not allowed for more than one person eating an identical number of pancakes. Aside from that, it works.
My question is whether it is possible to do this with only one array using, as the problem parameters indicate, only:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
I cannot figure out how to do this with only one array[10].
Can somone provide an idea of what intellectual leap I am missing? It does not seem like it should require two arrays. Or, is my solution reasonable enough?
My code is below.
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
|
// Write a program that asks the user to enter the number of pancakes eaten for
// breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
// Once the data has been entered the program must analyze the
// data and output a list in order of number of pancakes eaten of all 10 people.
// i.e.
// Person 4: ate 10 pancakes
// Person 3: ate 7 pancakes
// Person 8: ate 4 pancakes
// ...
// Person 5: ate 0 pancakes
#include<iostream>
using namespace std;
main()
{
int pancake[10]; // array to store pancakes eaten
int person[] = {1,2,3,4,5,6,7,8,9,10}; // array to store person #
int pancakeswap;
int personswap;
for (int i=0;i<10;i++)
{ // input loop
cout << "Enter the number of pancakes eaten by person " << (i+1) << endl;
cin >> pancake[i];
}
for (int j=0;j<10;j++)
{
int kmax = (10-j);
for (int k=1;k<kmax;k++)
{
if (pancake[(j+k)] > pancake[j])
{
pancakeswap = pancake[j]; // reordering pancake array
pancake[j] = pancake[(j+k)]; // "
pancake[(j+k)] = pancakeswap; // "
personswap = person[j]; // reordering person array
person[j] = person[(j+k)]; // "
person[(j+k)] = personswap; // "
}
}
}
cout << endl << endl;
for (int a=0;a<10;a++) // output list as expected
cout << "Person " << person[a] << " ate " << pancake[a] << "pancakes." << endl;
system("pause");
return 0;
}
|