usingnamespace std;
int persons_pancakes_eaten[10];
int persons_pancakes_copy[10];
int persons_ordered_by_pancakes[10];
int greatest_val = 0;
constint GREATEST = 0;
for (int i = 0; i < 10; ++i) {
cout << "Please enter person " << i + 1 << " pancakes eaten: ";
cin >> persons_pancakes_eaten[i];
persons_pancakes_copy[i] = persons_pancakes_eaten[i];
}
for (int CURRENT_GREATEST_PERSON = 0; CURRENT_GREATEST_PERSON < 10; CURRENT_GREATEST_PERSON++) {
greatest_val = 0;
for (int i = 0; i < 10; ++i) {
if (persons_pancakes_eaten[i] == -1)continue;
if (persons_pancakes_eaten[i] > greatest_val) {
greatest_val = persons_pancakes_eaten[i];
persons_ordered_by_pancakes[CURRENT_GREATEST_PERSON] = i;
}
}
int LAST_GREATEST = persons_ordered_by_pancakes[CURRENT_GREATEST_PERSON];
persons_pancakes_eaten[LAST_GREATEST] = -1;
}
for (int i = 0; i < 10; ++i) {
cout << "Person " << persons_ordered_by_pancakes[i]+1 << " ate " << persons_pancakes_copy[persons_ordered_by_pancakes[i]]<<'\n';
}
how I can improve this code, for example how should I name more appropriately variables, and how I can remove the second copy of the array I'm using there?
and how I can avoid the -1 for not valid array cell?
hopefully this is readable code and thanks for the help.
I'm a beginner.
You seem to be doing two things at once. The code would be much clearer if you did one loop to find the person who ate the most (and fewest) pancakes, and then did a separate loop to sort by the number of pancakes eaten.