Array problems

I'm stuck on the beginner exercise: Pancake Glutton
(http://www.cplusplus.com/forum/articles/12974/).
currently reading http://www.cplusplus.com/doc/tutorial/control/
I'm having problems with that i can't sort persons while remebering how many they ate.

please help with hints.

e: added highest/lowest value
New problem found:
if 2 persons eat the same highest/lowest amount everything goes wrong.
updated code to try to fix

e2:
just noticed that i don't get the same result in using C++Shell and from MVS C++
if i set that they ate from 0 -> 9. On C++Shell i get 9 max and 1 min while on MSV i get 8 max and 0 min
updated code with changes

e3: just noticed my sorting is completley messed up, trying to fix it.
updated code to see where everything is going wrong

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

int main()
{


	int person[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int ate[11];
	int highest;
	int lowest;

	for (int x = 0; x < 10; x++)
	{
			cout << "How many pancakes did person " << person[x] << " eat? ";
			cin >> ate[x];
			system("CLS");
	}

	for (int x = 0; x < 10; x++)
	{
		cout << "ate[" << x << "]" << " has value " << ate[x] << " at start."<< endl; //see if all identifiers has the right value
	}
	//sort ate by highest value
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (ate[j] < ate[j + 1])
			{
				highest = ate[j];
				ate[j] = ate[j + 1];
				ate[j + 1] = highest;
			}
		}
		cout << "ate[" << i << "]" << " after highest sort " << ate[i] << endl; // prints out the changes after sorting has occured. it should show higest at top, while lowest bottom.
	}
	cout << "The person who ate most pancakes ate: " << highest << " pancakes." << endl;
	//sort ate by lowest value
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (ate[j] > ate[j + 1])
			{
				lowest = ate[j];
				ate[j] = ate[j + 1];
				ate[j + 1] = lowest;
			}
		}
		cout << "ate[" << i << "]" << " after lowest sort " << ate[i] << endl; // prints out the changes after lower sorting has occured. it should show lowest at top, while highest bottom.
	}
	cout << "The person who ate the least pancakes ate: " << lowest << " pancakes." << endl;
	system("pause");
	return 0;
}
Last edited on
just noticed that i don't get the same result in using C++Shell
Do not believe C++Shell if you have any sort of interactivity. It has some serious bugs.

Do not use parallel arrays. It is hard to make straight. Combine both person number and amount of pancakes eaten in a single structure:
1
2
3
4
5
6
7
8
9
10
11
struct Person
{
    int number;
    int eaten;
};

//...

for (int j = 0; j < 10; j++)
    if (participants[j].eaten < participants[j + 1].eaten)
        std::swap(participants[i], participants[i+1])
i haven't learned about structs yet, are they easier to use when handling this type of code?.
the exercise says that i should be able to do this by using:
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

so it seems i have serious logic flaws :S, guess i'll restart on a empty project.
are they easier to use when handling this type of code?.
They make handling bunch of related variable an order of magnitude easier. Actually if you have to use parallel arrays (where you must support relations between different arrays), you done something wrong.
the exercise says that i should be able to do this by using:
It is for base excercise, not for star additions.
Base excercise can be easily solved with only one array, as you do not need to change it, only find highest and lowest values. Additional question require you to shift array around and this leads to the need to store person number alongside number of pancakes eaten.
Topic archived. No new replies allowed.