Trying to solve "Pancake Glutton" problem

closed account (jwC5fSEw)
The problem is found here: http://www.cplusplus.com/forum/articles/12974/

I really have no idea what to do with this. This seems to come pretty close:

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
#include <iostream>
using namespace std;

struct person {
	int num;
	int pancakes;
};

int main(){
	int min, max, most, least;
	person person[10];

	for (int n=0; n<10; n++){
		cout << "Enter the number of pancake eaten by person #" << n+1 << ": ";
		cin >> person[n].pancakes;
	}
	
	min = person[0].pancakes;
	max = person[0].pancakes;

	for (int n=1; n<11; n++){
		if(min > person[n].pancakes)
			min = person[n].pancakes;
			person[n].num = n;
			least = person[n].num;
		if(max < person[n].pancakes)
			max = person[n].pancakes;
			person[n].num = n;
			most = person[n].num;
	}

	cout << "The most pancakes were eaten by " << most << ", who ate " << max << ".\n";
}


It runs fine until the last line. It properly displays the number of pancakes inputted, but it just displays "most" as 9. I'm guessing that's because the array goes up to 9, but I can't figure out how to fix it. Any advice?
Your if statements are missing { } around them. And also, why do you need num to be part of person? You could just use n in the loops.
closed account (jwC5fSEw)
Yeah, I decided to use struct at the very beginning and by the time I got to the end I didn't really want to go back and change it. Removed the struct and changed it to this:

1
2
3
4
5
6
7
8
	for (int n=0; n<10; n++){
		if(min > person[n]){
			min = person[n];
			least = n+1; }
		if(max < person[n]){
			max = person[n];
			most = n+1; }
		}


It now works for all possibilities excluding person 1. If it's person 1, it displays -858993460 instead of 1. Why would that happen?
Oh, you are off by one on your array. Arrays go from 0-(size - 1), so your for loop should start a 0 and go to 9.
closed account (jwC5fSEw)
Yeah, I corrected that in the fixed one above.
I think your array is bad; you can't have a variable the same name as a struct. That shouldn't compile.

Usually if you get large, seemingly random numbers like that it's due to uninitialized data.
closed account (jwC5fSEw)
I removed the struct so that won't be a problem anymore. Also, you're right about the number; defining most as 0 gave me 0 instead of that number. I see what I did wrong: min = person[0], so if (min > person[0]) won't ever be true. Changing it to >= solved the problem. Thanks for the help, everybody!

EDIT: The final program, if anybody is interested:
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
#include <iostream>
using namespace std;

int main(){
	int min, max, most=0, least, person[10];

	for (int n=0; n<10; n++){
		cout << "Enter the number of pancake eaten by person #" << n+1 << ": ";
		cin >> person[n];
	}
	
	min = person[0];
	max = person[0];

	for (int n=0; n<10; n++){
		if(min >= person[n]){
			min = person[n];
			least = n+1; }
		if(max <= person[n]){
			max = person[n];
			most = n+1; }
		}

	cout << "The most pancakes were eaten by person #" << most << ", who ate " << max << ".\n";
	cout << "The least pancakes were eaten by person #" << least << ", who ate " << min << ".\n";
}
Last edited on
Topic archived. No new replies allowed.