Pancake Glutton help please

Hi everyone!

I'm trying very hard to solve an exercise and I think I've found the way,
but I don't know what's the problem in my code because it's not even
near of being correct... I've tried everything I know, for 2-3 days in a row.
Can someone, please, examine my code and tell me where am I wrong? :(

Here's the code, I've even translated it from RO to ENG :D please help...

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
#include <iostream>
using namespace std;

int main ()
{
	int person[2][10];
	int descending[2][10];
	int max;
	
	for (int i=0, n=1; i<10; i++, n++) // person number
	{
		person[0][i]=n;
	}

	cout << "Enter the number of pancakes eaten by every person:" << endl;
	
	for (int i=0; i<10; i++) // asking the user to input the pancakes
	{
		cout << "Person " << person[0][i] << ": ";
		cin >> person[1][i];
	}

	cout << endl << "A list from highest to lowest:" << endl;

	for (int i=0; i<10; i++)
	{
		max = 0;
		
		for (int i1=1; i1<10; i1++) // finding the highest number of pancakes eaten
		{
			if (person[1][i1] > max)
			{
				max=person[1][i1];
			}
		}

		for (int i2=0; i2<10; i2++) // copy the data from one matrix to another, in descending order
		{
			if (max == person[1][i2])
			{
				descending[0][i] = person[0][i2];
				descending[1][i] = person[1][i2];
				person[1][i2] = 0; 
			}
		}
	}

	for (int i=0; i<10; i++) // show the descending list
	{
		cout << "Person " << descending[0][i+1] << ": " << descending[1][i] << endl;
	}

	system ("pause");
	return 0;
}
cout << "Person " << person[0][i] << ": ";
cin >> person[1][i];

This seems to be wrong. You are asking the user to input person at position [0][i], but then input the data into position[1][i].
What is the output of your program right now. In reading through your code

1
2
3
4
5
6
7
8
9
		for (int i2=0; i2<10; i2++) // copy the data from one matrix to another, in descending order
		{
			if (max == person[1][i2])
			{
				descending[0][i] = person[0][i2];
				descending[1][i] = person[1][i2];
				person[1][i2] = 0; 
			}
		}


That just seems to find the person with the max number and then put them in the descending[][] and remove it from person[1][i2]. It should only put a single value in the descending array.

It may be easier to make this more modular and then call a findMax function after you "delete" the old max.

Can I ask why we have a 2D array for each person and descending?
hey i finished this recently...its okay you dont notice but at this stage you learn quite fast...i cant just give you my code thats all wrong but i can tell you i made an array of objects and then with the index number of the biggest fatty i could call their name as well...

when you solve it lets compare codes XD
You could make use of a bubble sort to place the items into ascending or descending order.
1
2
3
4
5
6
7
8
9
10
11
12
13
    // Bubble sort.
    for (int a = 0; a < size; ++a)        // Outer loop through entire array
    {
        for (int b = a+1; b < size; ++b)  // Inner loop starts at a + 1
        {
            if (array[b] > array[a])      // compare two items
            {
                int temp = array[a];      // swap the two items.
                array[a] = array[b];
                array[b] = temp;
            }
        }
    }


Because you have a 2-d array person[2][10] the code to swap the two items (lines 8,9,10) will take six lines rather than three.

I'd recommend as a good practice rather than hard-coding the value '10' multiple times throughout the program, you define a constant like this: const int size = 10;. That way, if you want to change the value, it's just one place in the code which requires change, instead of lots of them scattered throughout the code.
cout << "Person " << person[0][i] << ": ";
cin >> person[1][i];

This seems to be wrong. You are asking the user to input person at position [0][i], but then input the data into position[1][i].


person[0][i] is the person number :D and the user has to enter the number of pancakes (that's person[1][i]) ^_^

That just seems to find the person with the max number and then put them in the descending[][] and remove it from person[1][i2]. It should only put a single value in the descending array.


oh well it finds the person with the max value, transfers the data into the descending array and then "deletes" (0) the original value, so that it can find the next highest value, if you understand :D it's a loop

Can I ask why we have a 2D array for each person and descending?


well I have to output the person number as well :D like "Person 2: 456" then "Person 7: 253" etc.

when you solve it lets compare codes XD


I really hope I'll finish it xD I mean I hope I'll make it work, but at the moment I have no idea what's wrong...

and Chervil, thanks for the hint :D
and Chervil, thanks for the hint :D

The thing is, I looked at this code and did actually run it. But because I didn't quite understand the way it was supposed to work, I discarded much of the code and re-did it from scratch.

I could have posted my completed code, but I'm not sure whether that would really have helped, the only way to learn is to get your hands dirty and actually do the code yourself.
yeah you're right... I'm gonna thing of another way to do the program or something else :) I really don't want "spoilers" xD I just wanted to know why the heck it's not working because I'm really sure it should work...
you done it yet?
Topic archived. No new replies allowed.