Pancakes!

Hello. On the final star of Pancake Glutton

http://www.cplusplus.com/forum/articles/12974/

I cannot figure out how to go about telling the computer to print the list in order form least to greatest. I feel pretty ashamed. I actually had quite a difficult time figuring out how to make the bare bones program work. Its way hard.
Some help? Thanks
Hm. well i think you can use a for loop and nested if-else statements for that. I haven't figured out an exact code yet but I'm pretty sure you'll need to use it.

Hmm yeah the specifics elude me though. Ive been thinking about it for a while. maybe it will be like learning i++; once you see it you'll get it forever, but damn hard to figure out on your own...
Well i think you can use a for loop to enter values for the 10 persons but printing them in order is a bit tricky. Since in that section you aren't allowed to use vectors then it's harder to sort them.
Yah i already completed the rest of the program except that last star. http://pastebin.com/aZ1YW0Fh - thats what i got done.

I will probably sit down again for it seriously tommorow but... *shakes fists*
A lot of patience is needed for C++ in the meantime I'll ponder on your problem as well and see if I can come up with something similar to the last star.
This is what I've written for the third part of the pancake glutton program.

I have not allowed for more than one person eating an identical number of pancakes. Aside from that, it works.

My question is whether it is possible to do this with only one array using, as the problem parameters indicate, only:

variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

I cannot figure out how to do this with only one array[10].

Can somone provide an idea of what intellectual leap I am missing? It does not seem like it should require two arrays. Or, is my solution reasonable enough?

My code is below.

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

#include<iostream>
using namespace std;

main()
{
int pancake[10]; // array to store pancakes eaten
int person[] = {1,2,3,4,5,6,7,8,9,10};  // array to store person #
int pancakeswap;
int personswap;

for (int i=0;i<10;i++)
    {       // input loop
        cout << "Enter the number of pancakes eaten by person " << (i+1) << endl;
        cin >> pancake[i];
    }

for (int j=0;j<10;j++)      
    {  
    int kmax = (10-j);
    for (int k=1;k<kmax;k++) 
        {
        if (pancake[(j+k)] > pancake[j])
            {
             pancakeswap = pancake[j];      // reordering pancake array
             pancake[j] = pancake[(j+k)];   //          "
             pancake[(j+k)] = pancakeswap;  //          "
             personswap = person[j];        //  reordering person array
             person[j] = person[(j+k)];     //          "
             person[(j+k)] = personswap;    //          "
            }
        }
    }
       
       cout << endl << endl;
       
         
 for (int a=0;a<10;a++)         // output list as expected
 cout << "Person " << person[a] << " ate " << pancake[a] << "pancakes." << endl;
     
system("pause");
return 0;
}
You could use classes to group the data together then sort an array of Person or the like.
can only use:

variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

I understand what you are saying but I have two counters. One is nitpicky.

1. It says required, not "can only use"
2. Using something additional you would have to learn (in this case, classes), could only help. You are doing thing as an academic exercise, anyway, so I see no reason not to use everything you've learned to make the problem easier.
Here is my solution, it works like a charm, except if the user enters a floating variable, the loops go nuts. Anybody know why?

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
#include "stdafx.h"
#include <iostream>
#include <algorithm> //For swap

int main()
{
	//Here I create the index and the index size. Index anNumber[n][0] will be used for storing the persons number,
	//anNumber[n][1] will be for storing the amount of pancakes.
	const int nIndexSize = 10;
	const int nPerson = 0;
	const int nPancakes = 1;
	int anNumber[nIndexSize][2] = {0};

	//Here I get information from the user and store it in the index
	using namespace std;
	int nInput = 0;
	for (int nCount = 0; nCount < nIndexSize; nCount++)
	{
		cout << "Enter amount of pancakes for person #" << nCount + 1 << ": ";
		cin >> nInput;
		anNumber[nCount][nPerson] = nCount + 1;
		anNumber[nCount][nPancakes] = nInput;

		//If user input an invalid value, he will be prompted to enter it again
		if (nInput < 0)
			nCount--;
	}
	cout << endl;

	//Here I find the highest and lowest value in anNumber[] 
	int nLowestValue = 0;
	int nHighestValue = 0;
	for (int nCount = 0; nCount < nIndexSize; nCount++)
	{
		if (anNumber[nCount][nPancakes] > anNumber[nHighestValue][nPancakes])
			nHighestValue = nCount;
		if (anNumber[nCount][nPancakes] < anNumber[nLowestValue][nPancakes])
			nLowestValue = nCount;
	}
	cout << "Person #" << anNumber[nHighestValue][nPerson] << 
		" ate the most at " << anNumber[nHighestValue][nPancakes] << " pancakes./n";

	cout << "Person #" << anNumber[nLowestValue][nPerson] << 
		" ate the least at " << anNumber[nLowestValue][nPancakes] << " pancakes. /n/n";

	//Here I sort the array in a descending order, swapping both nPerson and nPancakes
	for (int nStartIndex = 0; nStartIndex < nIndexSize; nStartIndex++)
	{
		int nLargestIndex = nStartIndex;
		for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < nIndexSize; nCurrentIndex++)
		{
			if (anNumber[nCurrentIndex][nPancakes] > anNumber[nLargestIndex][nPancakes])
				nLargestIndex = nCurrentIndex;
		}

		swap (anNumber[nStartIndex][nPancakes], anNumber[nLargestIndex][nPancakes]);
		swap (anNumber[nStartIndex][nPerson], anNumber[nLargestIndex][nPerson]);
	}

	//Here I print a descending list of persons and pancakes
	for (int nCount = 0; nCount < nIndexSize; nCount++)
	{
		cout << "Person #" << anNumber[nCount][nPerson] << " ate " << anNumber[nCount][nPancakes] << " pancakes./n";
	}
	cout << endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.