Question
So I found this practice problem for C++ and I understand everything exept how to do the last modification it asks for. I'll need to
So I found this practice problem for C++ and I understand everything exept how to do the last modification it asks for. I'll need to refresh my memory on how to do it but would a bubblesort be the way to go? Also I had to set max_per = 1 in order for it to print out "Person No. 1 ate the most pancakes!" for some reason. Other wise it would give a bunch of random numbers. And one final thing, given that we're suppose to use variables, data types, and numerical operators, basic input/output, logic (if statements, switch statements), loops (for, while, do-while), and arrays, is this an efficient way to write this kind of program?
The assignment:
Pancake Glutton
Requires: variables, data types, and numerical operators basic input/output logic (if statements, switch statements) loops (for, while, do-while) arrays 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 which person ate the most pancakes for breakfast. Modify the program so that it also outputs which person ate the least number of pancakes for breakfast. 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
Here's my code:
#include
using namespace std;
int main()
{
int ate[10]; // array for counting pancakes eaten
int max_ate; // var for most pancakes eaten
int max_per = 1; // var for person who ate the most (why = 1?)
int min_ate; // var for least pancakes eaten
int min_per; // var for person who ate the least
cout << "How many pancakes did each person eat? ";
for (int i = 0; i < 10; i++)
{
cout << "Person " << i + 1 << " ate how many pancakes? ";
cin >> ate[i];
}
// Setting the variable for the most pankakes eaten to the first element entered in the array
// This is also saying that if nothing is greater than ate[0] in the following for loop, then
// that will be the greatest number of pancakes eaten because all of the other elements in
// the for loop will have failed to be true.
max_ate = ate[0];
for (int j = 0; j < 10; j++)
{
if (max_ate < ate[j])
{
max_ate = ate[j];
max_per = j + 1;
}
}
// Now looking for the person who ate the least
for (int j = 0; j < 10; j++)
{
if (min_ate > ate[j])
{
min_ate = ate[j];
min_per = j + 1;
}
}
cout << "Person No. " << max_per << " ate the most: " << max_ate << " Pancakes! ";
cout << "Person No. " << min_per << " ate the least: " << min_ate << " Pancakes! ";
for (int k = 0; k < 10; k++)
{
cout << ate[k] << endl;
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started