Pancake glutton exercise!

Hi all!

I've been doing the pancake glutton exercise as seen here:
http://www.cplusplus.com/forum/articles/12974/

and I'd really appreciate if you could take the time to look at my code and tell me hints on how I could improve it:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include using std::cin; using std::cout; using std::endl; int main() { int Group[9]; int MostEaten; int LeastEaten; for(int index = 0; index < 10; ++index) { cout << "Please input how many pancakes Person #" << index + 1 << " has eaten: " << endl; int EatenPancakes = 0; cin >> EatenPancakes; Group[index] = EatenPancakes; if(index == 0) { cout << "A new record!" << endl; MostEaten = index; } else if(Group[index] < Group[index - 1] && index != 0) { LeastEaten = index; } else if(Group[index] > Group[index - 1] && index != 0) { MostEaten = index; cout << "A new record!" << endl; } } cout << "The person who has eaten the most pancakes is Person #" << MostEaten + 1 << endl; cout << "The person who has eaten the least pancakes is Person #" << LeastEaten + 1 << endl; return 0; }``````

Now I'm trying to design on paper the second variant of the problem:

(★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people),

but before I even touch any code, I'm kind of scratching my head as to how I could solve it.

Any hints? So far I've been thinking about using multidimensional arrays or even a struct.

Last edited on
 I'm kind of scratching my head as to how I could solve it.

You're almost there. You have an array of how many pancakes each person ate. Now you just need to sort it. If you want to preserve the person number and pancakes eaten mapping, you could use a struct. Just make it hold person number and pancakes eaten. Would barely take any modifications to accommodate that.

 how I could improve it

1) Allow a variable number of people to eat pancakes (maybe via command line argument)
2) Use something safer than a raw array. I'd do this one first.
3) Input exception handling.
4)
 ``12345`` ``````if(index == 0) { cout << "A new record!" << endl; MostEaten = index; }``````

This output statement seems odd. First iteration it's going to be a "new record" even though it's the only input so far.

Do these, come back and post the new code and we'll critique it again. I urge you to research these on your own for a bit, and then if you get stuck come back and ask questions as to how to do some of these tips.
Hi ResidentBiscuit,

Thanks for your time, it's much appreciated.

 Do these, come back and post the new code and we'll critique it again. I urge you to research these on your own for a bit, and then if you get stuck come back and ask questions as to how to do some of these tips.

Absolutely! I had no intention whatsoever of being spoon fed the answers.

1) Good idea, fairly easy though and I didn't do it because the exercise specified 10 people only;

2) I'll need to research a bit by myself what you mean by "raw" array, and why it isn't safe. First time I hear about arrays not being safe to be honest! I was thinking of using a vector in the first place because I know they can grow dynamically.

3 & 4) I'll look into that.

I appreciate your answers, and I will come back with an improved solution, I hope you can come back and take a look again after.

Thanks!
Raw arrays (ie not std::array of C++11) provide no support of any kind. Std::vector is almost always the better choice when using C++.
Hi everyone,

So here's the new code which includes ResidentBiscuit's revisions. I decided to completely scrap the old code and start from scratch using Structs and use the sort() function to determine who ate the most and least pancakes.

Again, any suggestions on how I can improve it is very welcome.

Here goes:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include #include #include using std::cin; using std::cout; using std::endl; using std::vector; struct ContestantProfile { int contestant_number; int pancakes_eaten; }; bool winner(const ContestantProfile& x, const ContestantProfile& y) { return x.pancakes_eaten > y.pancakes_eaten; } int main() { vector contestant; cout << "How many contestants is there?" << endl; int ContestantNumberTotal; cin >> ContestantNumberTotal; for(int index = 0; index < ContestantNumberTotal; ++index) { contestant.push_back(ContestantProfile()); cout << "Please input how many pancakes Person #" << index + 1 << " has eaten: " << endl; int EatenPancakes = 0; bool ValidEntry = false; while(!ValidEntry) { ValidEntry = true; cin >> EatenPancakes; if(EatenPancakes <= 0) { cout << "You have entered an invalid amount of pancakes. Please try again." << endl; int EatenPancakes = 0; ValidEntry = false; } } contestant[index].contestant_number = index + 1; contestant[index].pancakes_eaten = EatenPancakes; } sort (contestant.begin(), contestant.end(), winner); cout << "Winner is Contestant # " << contestant[0].contestant_number << ", who ate " << contestant[0].pancakes_eaten << " pancakes." << endl; cout << "Loser is Contestant # " << contestant[ContestantNumberTotal - 1].contestant_number << ", who ate " << contestant[ContestantNumberTotal - 1].pancakes_eaten << " pancakes." << endl; return 0; }``````
Sorry for the slow reply. Was on break and was much busier than I expected. This looks good though. You're using a lot of C++ features, it's readable, looks more or less bug free. I'd say move on to the next problem and use the new knowledge you picked up.
Topic archived. No new replies allowed.