I think that the issue lies on lines 19-27 and i know it's probably a logic error but for whatever reason I am unable to see it. I have used the debugger and I understood what was going on i know that it was skipping some person and I didn't know why it was doing that so I don't know how to fix it. One other thing is that the exercise is the Pancake Glutton and it is the last modification.
oops i changed that on my code on my visual studios... I originally thought that i didn't want to repeat, but later realized that it would not work correctly...
Why do you make it so complicated?
You could create a struct to store the number of the person and number of pancakes.
1 2 3 4 5
struct PancakeInfo
{
int NumberOfPerson;
int NumberOfPancakes Eaten;
}
Then you need only one array with the structs.
Finding the min and max of this array should be easy.
Then you sort the array in descending order and dump it on the screen.
#include <iostream>
#include <array>
struct Person
{
int index;
int cakes;
};
int main()
{
std::array<Person, 10> person;
//INPUT
for (int i = 0; i < 10; i++) {
std::cout << "How many pancakes did person ";
std::cout << i;
std::cout << " eat? ";
std::cin >> person[i].cakes;
person[i].index = i;
}
//MAXIMUM
int maximum = person[0].cakes;
int maxPerson = 0;
for (int i = 0; i < 10; i++)
{
if (maximum < person[i].cakes)
{
maximum = person[i].cakes;
maxPerson = i;
}
}
std::cout
<< "Maximum eaten by person " << maxPerson
<< " who ate " << person[maxPerson].cakes
<< std::endl;
system("pause");
return 0;
}
★★★★ Modify the program so that it outputs 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
@kemort: Although, I appreciate the full blown answer, it wasn't quite what I was looking for.
I just wanted to have help in using my code and getting some insight on what I was missing or not understanding.
I just wanted to have help in using my code and getting some insight on what I was missing or not understanding.
That's OK I did it for my own interest. :]
@kemort: Does your code even produce the correct output? When I ran your program (with the following modifications) it didn't seem to produce the correct results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
...
std::array<Person, 10> person{10,3,6,4,33,21,44,54,14,18};
/*
//INPUT
for (int i = 0; i < 10; i++) {
std::cout << "How many pancakes did person ";
std::cout << i;
std::cout << " eat? ";
std::cin >> person[i].cakes;
person[i].index = i;
}
*/
...
Result: Maximum eaten by person 3 who ate 54
The 54 is correct by it should be person 8.
@Thomas1965: it's OK i forgot to write out the requirements, which was my fault. You did nothing wrong. In hindsight, I should have added the list of what things it required and the link to the page. :]
@kemort: Sorry about sounding harsh i just wanted to get a better understanding on what I wasn't understanding. Yeah I think that might work and I think I will mark this as solved. If I still have trouble I'll just unmark it as resolved.
• variables, data types, and numerical operators
• basic input/output
• logic ( if statements, switch statements )
• loops ( for, while, do-while )
• arrays
• pseudo random number generation
• strings & string functions
• functions
• structures/classes
• enumerated data
• file input/output
• pointers
• sorting
• linked lists
• advanced classes
• recursion
Shouldn't be taken as absolutes. For example if you were to follow this list to the letter you probably shouldn't be using std::array yet, unless you consider std::array, std::vector, std::string, etc to be discussed in the first point. I myself think std::vector and std::string should be used very early, std::array not a big fan.
I myself would recommend functions be learned right after loops since I consider functions more important than the following items.
Keep us informed on your progress with the program.
Edit: Also the last part of this exercise could be considered sorting since you need to show the data in "sorted" order maximum to minimum.