Need help with beginner exercises.

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
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>

using namespace 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;
}
Hi,

basically you need to find the largest value in a array

there's a lot of resource for that online but it would be better if you figure that out yourself
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'.
Last edited on
Alright, thanks for the help!
Topic archived. No new replies allowed.