using arrays

I need some help with the output of my program and using arrays.
Here's the problem:

★★★★ 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


I can't figure out how to use an array to display the person and the largest number in descending order. I'm almost positive that I have to use the same array in a for loop in conjunction with itself to get the people, but I'm stuck after that.

I've tried nesting a for loop within the for loop but that ended horribly.

Here's the code so far:
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
#include <iostream>

using namespace std;

double panAverage(double avg, int total){ // calculates average of pancakes eaten
    double average = 0;
    average = total / avg;
    return average;
}
int main()
{
    const int avg = 10;
    int pancakes[10], total = 0, average, max1 = 0, num = 0, max2;
    for(int i = 1; i <= 10; i++){
            cout << "How many pancakes did person [" << i
                << "] eat? ";
            cin >> pancakes[num];
            total += pancakes[num];         // gets total of pancakes
            if (pancakes[num] > max1)       // gets max pancakes
                max1 = pancakes[num];
            // I think using something like pancakes[i] would work to get the people but I'm not sure what to do next
    }
    cout << "There were a total of " << total << " pancakes eaten\n";
    cout << "The average number of pancakes eaten was " << panAverage(avg, total) << endl;
    cout << "The most pancakes that anyone ate was " << max1;
    return 0;
}


Thanks for the help.
Why does pancakes have room for 10 int if you only use the first one?
I'll be honest, I don't quite understand. I was under the impression that the array pancakes can have no more than 10 integers...therefore [num] cannot count past 10.
num is 0 for the entire program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    const unsigned nPeople = 10 ;

    unsigned pancakes[nPeople] ;

    for ( unsigned i=0; i<nPeople; ++i )
    {
        std::cout << "How many pancakes did person " << i+1 << " eat? " ;
        std::cin >> pancakes[i] ;
    }

    for ( unsigned i=0; i<nPeople; ++i )
        std::cout << "Person " << i+1 << " ate " << pancakes[i] << " pancakes.\n" ;
}
Last edited on
Topic archived. No new replies allowed.