Need help with array excercise

Hello everybody.

I'm doing this exercise, and I've gotten as far as to sorting.. I have some problems though as described below the exercise.

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

My code till now:

My problems:
1. I'm stuck at saying which person ate the max and min number of pancakes. Right now person 10 ate both min and max number of pancakes

2. The sorting part. I need to say which person ate the number of pancakes in order, not just 1), 2), 3) etc. as i did

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>

using namespace std;

int main()
{
    int i;
    int Pancakes[10];
    int max;
    int min;

    cout << "Enter pancakes eaten by people:\n";
    cin >> Pancakes[0];
    max = Pancakes[0];
    min = Pancakes[0];

    for(i = 1; i < 10; i++)
    {
        cin >> Pancakes[i];
        if(Pancakes[i]>max)
            max = Pancakes[i];

        else if(Pancakes[i]<min)
            min = Pancakes[i];
    }

    // Max and min number of eaten pancakes

    cout << "The highest eaten number of pancakes is " << max << " by person " << i << endl << endl;
    cout << "The lowest eaten number of pancakes is " << min << " by person " << i << endl << endl;

    // Saying how many pancakes the people ate

    for(i = 0; i < 10; i++)
        cout << "Person " << i << " ate: " << Pancakes[i] << " pancakes" << endl;


     //Sorting the number of eaten pancakes

    for (int i =9 ;  i >= 0 ; i--)
	{
		for (int  x = 0 ; x< 10; x++)
		{
			if(Pancakes[x]>Pancakes[x+1])
			{

				int temp=Pancakes[x+1];

				Pancakes[x+1]=Pancakes[x];

				Pancakes[x]=temp;

			}
		}
	}

	cout<<"\n---Sorted---\n\n";

	for(int i=0; i<10; i++)
	{
		cout<<i+1<<") "<<Pancakes[i]<<endl;
	}

	cin.ignore();



    return 0;
}
You do have essentially this:
1
2
3
4
5
int foo;
for ( foo = 1; foo < 10; foo++) {
}
cout << "max by person " << foo << endl;
cout << "min by person " << foo << endl;

How is the foo related to the amount?

Your min and max keep track of how much was eaten. Perhaps they should point to the eaters instead?


On last iteration of loop on line 42 the x has value 9. The if-clause dereferences element x+1, i.e. Pancakes[10], which is out of range. Perhaps the loop should iterate one time less?
Hello Elerobo,

First you should initialize all your variables to avoid problems later and it is just good practice. max will work best being set to zero and I am thinking that min might work best is the starting value is 10.

You will need an array to deal with names. This does make the sort function more interesting.

Line 12 works better inside the for loop. This makes line 13 unnecessary. Lines 14 and 15 should not be there initializing the min and max when defined makes these lines unnecessary and nor the right way to give these variables a value.

The first for loop should also deal with entering names.

The sort function will need to be reworked, but I would deal with input first.

Hope that helps,

Andy

After further investigation I found the sort portion did not work as you have it. It produced a run time error when it accessed the Pancake array outside of its boundary i.e., with a subscript of 10 when the largest number should be 9.
Last edited on
Topic archived. No new replies allowed.