Pancake glutton

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


Need some help with the last one. I have no idea how to sort the value from greatest to least.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int person[5] = { 1, 2, 3, 4, 5 };
	int panCakes[5];

	int thePerson;
	int theGreatest = 0;

	for (int x = 0; x < 5; x++)
	{
		cout << "How many pancakes did person " << person[x] << " eat?";
		cin >> panCakes[x];
		
		if (panCakes[x] > theGreatest)
		{
			theGreatest = panCakes[x];
			thePerson = person[x];
		}
	}

	cout << "Person " << thePerson << " ate " << theGreatest << " pancakes!!!\n";

Getting the highest and lowest is pretty simple, take a look at this code.

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

#include <iostream>
using namespace std;

int main()
{
	int person[5] = { 1, 2, 3, 4, 5 };

	// declare two variables and assign
	// them a default value from your
	// list of numbers.
	int lowest = person[0], highest = person[0];

	for (int i = 0; i < 5; i++) {
		// get highest, if the next read value is
		// higher than the last, assign highest
		// to the new value
		if (person[i] > highest)
			highest = person[i];
		// now check for the lowest value using the
		// same technique.
		if (person[i] < lowest)
			lowest = person[i];
	}

	cout << "Lowest is " << lowest << endl;
	cout << "Highest is " << highest << endl;

	return 0;
}

Lowest is 1
Highest is 5
Thats not what i need help with. What i need help with i to sort who ate the most, who ate the 2nd most, who ate the 3rd etc etc....
and the output would be like

person 3 ate 5000 pancakes.
person 2 ate 3000 pancakes.
person 1 ate 5 pancakes.
person 5 ate 3 pancakes.
person 4 ate 0 pancakes.



Sorry I realised that after I posted. Sorting in descending order added. If you have any questions please ask.

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

#include <iostream>
using namespace std;

int main()
{
	int person[5] = { 12, 6, 14, 22, 31 };
	int temp;

	// declare two variables and assign
	// them a default value from your
	// list of numbers.
	int lowest = person[0], highest = person[0];

	for (int i = 0; i < 5; i++) {
		// get highest, if the next read value is
		// higher than the last, assign highest
		// to the new value
		if (person[i] > highest)
			highest = person[i];
		// now check for the lowest value using the
		// same technique.
		if (person[i] < lowest)
			lowest = person[i];
	}

	cout << "Lowest is " << lowest << endl;
	cout << "Highest is " << highest << endl << endl;

	// now lets sort them in descending order
	for (int i = 0; i<5; i++)
	{
		for (int j = 0; j<5; j++)
		{
			if (person[i] > person[j])
			{
				temp = person[j];
				person[j] = person[i];
				person[i] = temp;
			}
		}
	}

	// display them.
	for (int i = 0; i < 5; i++)
		cout << person[i] << endl;

	return 0;
}

Lowest is 6
Highest is 31

31
22
14
12
6
This code sorts out the amount of pancakes from greatest but what about telling it which person ate how much? Like rather then just printing out the number from greatest to least, how would you write it so it also prints out the person who ate that amount?EX:
person 5 ate 31 pancakes.
person 2 ate 22 pancakes.
person 3 ate 14 pancakes.
person 1 ate 12 pancakes.
person 4 ate 6 pancakes.

You'll be sorting two arrays based on the content of one. Just remember that parallel elements of the arrays must be moved (swapped) in tandem.
Uhh how would i do that? I there a way for to make it so that i can store 2 values? The 1st value would the person's and the 2nd value would be the amount of pancakes and these 2 values will be linked so that when i print out the amount of pancakes from greatest to least, it will also output the persons number.
I would do something like 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
#include <stdio.h>

struct peoplecakes {
	int person;
	int cakes;
};

int main(int argc, char **argv) {
	peoplecakes count[5] = {{1,12},{2,6},{3,14},{4,22},{5,31}};
	fprintf(stdout,"BEFORE:\n");
	for(int i=0;i<5;i++)
		fprintf(stdout,"person: %i,cakes %i\n",count[i].person,count[i].cakes);
	for(int min=0,max=5;min<max;min++){
		int minDEX=min,maxDEX=--max;
		for(int j=min;j<=max;j++){
			minDEX = count[minDEX].cakes>count[j].cakes?j:minDEX;
			maxDEX = count[maxDEX].cakes<count[j].cakes?j:maxDEX;
		}
		if(count[minDEX].cakes==count[maxDEX].cakes)
			break;
		
		peoplecakes temp = count[min];
		count[min] = count[minDEX];
		count[minDEX] = temp;
		
		temp = count[max];
		count[max] = count[maxDEX==min?minDEX:maxDEX];
		count[maxDEX==min?minDEX:maxDEX] = temp;
	}
	fprintf(stdout,"AFTER:\n");
	for(int i=0;i<5;i++)
		fprintf(stdout,"person: %i,cakes %i\n",count[i].person,count[i].cakes);
	return 1;
}



BEFORE:
person: 1,cakes 12
person: 2,cakes 6
person: 3,cakes 14
person: 4,cakes 22
person: 5,cakes 31
AFTER:
person: 2,cakes 6
person: 1,cakes 12
person: 3,cakes 14
person: 4,cakes 22
person: 5,cakes 31

take note though that there are many ways to go about this.

@ youare29 I don't think he can use structures.

In this example I have used a 2D array and modified the sorting code - hope it helps

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

#include <iostream>
using namespace std;

int main()
{
	// create a 2 dimensional array, Ive gave it some
	// values, saves clearning it out with a loop
	int pancakes[2][10] = {
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10,   // row 0
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0     // row 1 (where qty will be)
	};

	int numPerson = 0, qtyPancakes = 0;  // for sort

	// lets read in 10 numbers
	for (int i = 0; i < 10; i++) {
		cout << "Enter number of pancakes for person " << i+1 << ": ";
		cin >> pancakes[1][i]; // row 1 column i (refer to 2d array)
	}

	// sort them.
	for (int i = 0; i<10; i++)
	{
		for (int j = 0; j<10; j++)
		{
			// check qty ate value
			if (pancakes[1][i] > pancakes[1][j])
			{
				numPerson = pancakes[0][j];  
				qtyPancakes = pancakes[1][j];
				pancakes[0][j] = pancakes[0][i];
				pancakes[1][j] = pancakes[1][i];
				pancakes[1][i] = qtyPancakes;
				pancakes[0][i] = numPerson;
			}
		}
	}

	// display them
	for (int i = 0; i < 10; i++)
		cout << "Person " << pancakes[0][i] << " ate " << pancakes[1][i] << " pancakes." << endl;

	return 0;
}

Enter number of pancakes for person 1: 55
Enter number of pancakes for person 2: 43
Enter number of pancakes for person 3: 36
Enter number of pancakes for person 4: 21
Enter number of pancakes for person 5: 77
Enter number of pancakes for person 6: 53
Enter number of pancakes for person 7: 67
Enter number of pancakes for person 8: 26
Enter number of pancakes for person 9: 10
Enter number of pancakes for person 10: 7

Person 5 ate 77 pancakes.
Person 7 ate 67 pancakes.
Person 1 ate 55 pancakes.
Person 6 ate 53 pancakes.
Person 2 ate 43 pancakes.
Person 3 ate 36 pancakes.
Person 8 ate 26 pancakes.
Person 4 ate 21 pancakes.
Person 9 ate 10 pancakes.
Person 10 ate 7 pancakes.
Topic archived. No new replies allowed.