I'm doing the beginner exercises found here: http://www.cplusplus.com/forum/articles/12974/. I'm specifically doing Pancake Glutton. I've taken two C++ classes so far in college and I didn't pay very much attention in them, so I'm relearning everything before I take a third one. My specific question is regarding this part of the problem, "Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast." I was setting it up in a way with continuous if statements comparing each case, etc. I'm pretty sure there is a much easier way to do this, but I'm unsure how. Can anyone give me some insight on a better way to approach this?
/*
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.
*/
#include <iostream>
#include <cmath>
usingnamespace std;
int main() {
//variables
int x;
int pancake[10];
//loop to ask for vaules up until 10.
for (x = 0; x < 10; x++) {
cout << "Please enter the amount of waffles that 10 people ate." << endl;
cin >> pancake[x];
}
//print out for who ate the most waffles
if (pancake[0] > pancake[1]) {
if (pancake[0] > pancake[2]) {
cout << "Person 1 ate the most waffles. The amount of waffles they ate are: " << pancake[0] << endl;
}
}
system("pause");
return 0;
}
Definitely not the best way to do this. You need to find the max # of pancakes eaten and the person (i.e. array index) associated with that.
Here's how you'd do that.
Pseudocode:
1 2 3 4 5 6 7 8 9
max = arr[0] // set max to 0th element of array
max_person = 0 // 0th person at the most pancakes
//traverse through array
for i = 0 to end of array
if(arr[i] is greater than max)
make it the new max;
max_person = i;
end loop:
By the time we come out of the loop, 'max' will hold maximum # of pancakes and 'max_person' will hold the array index associated with 'max'.