Pancake glutton 4 stars last step

Hello, I have been coding with c++ for about 2-3 weeks now and I have been stuck with one exercise called "Pancake glutton" I need to collect answers from 10 people how many pancakes have they ate for breakfast and output answers in order with their respective number like that:

Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

I have found a way how to put out the pancake amounts in decreasing order, but I still need to find out how to associate pancake amount with persons number.

I would appreciate help. Thank you.


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
 #include<iostream>

using namespace std;

int main(){

int amount[10], number;

for(number=0;number<=9;number++){
    cout<<"How many pancakes does person # "<<number+1<<" ate?"<<endl;
    cin>>amount[number];

}

    for(int a=0;a<=8;a++){
        int temp;

        for(int b=a+1;b<=9;b++){
                if(amount[a]>amount[b]){
                    temp=amount[a];
                    amount[a]=amount[b];
                    amount[b]=temp;

                }
        }
    }


    cout<<endl;

    for(int c=0;c<=9;c++){
        cout<<amount[c]<<" pancakes"<<endl; //I need to find a way to somehow tie pancake amount and persons number together
    }



}
@JustinPlusPlus you should make a struct that can hold each person and their number of cancakes.

Here is an attempt to your problem.
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
#include <iostream>

struct Person
{
	int numberOfPancakes = 0;
};

int main()
{
	const int NUM_OF_PERSONS = 10;

	Person persons[NUM_OF_PERSONS];

	std::cout << std::endl;
	for (int i = 0; i < NUM_OF_PERSONS; i++){
		std::cout << "How many pancakes does person #" << (i + 1) << " eat ? ";

		std::cin >> persons[i].numberOfPancakes;
	}

	std::cout << std::endl << std::endl;
	for (int i = 0; i < NUM_OF_PERSONS; i++){
		std::cout << "Person " << (i + 1)
			      << " : ate " << persons[i].numberOfPancakes 
                              << " pancakes"<< std::endl;
	}
	std::cout << std::endl;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.