Determine the largest value within an array

So i've been doing the pancake glutony beginner exercise. And i've made the part
of this code where the user enters the number of pancakes each person has eatenin an array work. However i have no clue as to how to determine the largest
number within the array the best way so i tried 10 while loops, but they give me the error message:

Run-Time Check Failure #2 - Stack around the variable 'person' was corrupted.

Do you know any way to resolve this problem?
Also if you'd teach me a much more efficient way to determine the largest value within the array, i'd be grateful as i'm sure this is way over the top.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include<iostream>
using namespace std;

int main() {
	
	int temp = 0;
	int sitrep = 1;
	int person [10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [1];
			sitrep++;
	}
	
	while (sitrep == 1);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [2];
			sitrep++;
	}
	
	while (sitrep == 2);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [3];
			sitrep++;
	}
	
	while (sitrep == 3);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [4];
			sitrep++;
	}
	
	while (sitrep == 4);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [5];
			sitrep++;
	}
	
	while (sitrep == 5);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [6];
			sitrep++;
	}
	
	while (sitrep == 6);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [7];
			sitrep++;
	}
	
	while (sitrep == 7);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [8];
			sitrep++;
	}
	
	while (sitrep == 8);

		do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [9];
			sitrep++;
	}
	
	while (sitrep == 9);

			do {
		cout << "Enter how many pancakes person " << sitrep << " ate." << endl;
		cin >> person [10];
			sitrep++;
	}
	
	while (sitrep == 10);

	while (person [1] > person [2],person [1] > person [3],person [1] > person [4],person [1] > person [5], // Checks if person [1] is the largest value in the person array.
			person [1] > person [6],person [1] > person [7],person [1] > person [8],person [1] > person [9], person [1] > person [10]) {

	cout << "The person that ate the most pancakes is " << "person 1!" << endl; }

	while  (person [2] > person [1],person [2] > person [3],person [2] > person [4],person [2] > person [5], // Checks if person [2] is the largeset value in the person array.
			person [2] > person [6],person [2] > person [7],person [2] > person [8],person [2] > person [9], person [2] > person [10]) {

			cout << "The person that ate the most pancakes is " << "person 2!" << endl;

	while (person [3] > person [2],person [3] > person [1],person [3] > person [4],person [3] > person [5], // Checks if person [3] is the largest value in the person array.
			person [3] > person [6],person [3] > person [7],person [3] > person [8],person [3] > person [9], person [3] > person [10]){

		cout << "The person that ate the most pancakes is " << "person 3!" << endl; }


	while (person [4] > person [2],person [4] > person [1],person [4] > person [3],person [4] > person [5], // Checks if person [4] is the largest value in the person array.
			person [4] > person [6],person [4] > person [7],person [4] > person [8],person [4] > person [9], person [4] > person [10]) {

				cout << "The person that ate the most pancakes is " << "person 4!" << endl; }

	while (person [5] > person [2],person [5] > person [1],person [5] > person [3],person [5] > person [4], // Checks if person [5] is the largest value in the person array.
			person [5] > person [6],person [5] > person [7],person [5] > person [8],person [5] > person [9], person [5] > person [10]){

				cout << "The person that ate the most pancakes is " << "person 5!" << endl; }

	while (person [6] > person [2],person [6] > person [3],person [6] > person [4],person [6] > person [5], // Checks if person [6] is the largest value in the person array.
			person [6] > person [1],person [6] > person [7],person [6] > person [8],person [6] > person [9], person [6] > person [10]){

				cout << "The person that ate the most pancakes is " << "person 6!" << endl; }

	while (person [7] > person [1],person [7] > person [3],person [7] > person [4],person [7] > person [5], // Checks if person [7] is the largeset value in the person array.
			person [7] > person [6],person [7] > person [2],person [7] > person [8],person [7] > person [9], person [7] > person [10]) {

				cout << "The person that ate the most pancakes is " << "person 7!" << endl; }

	while (person [8] > person [2],person [8] > person [1],person [8] > person [4],person [8] > person [5], // Checks if person [8] is the largest value in the person array.
			person [8] > person [6],person [8] > person [7],person [8] > person [3],person [8] > person [9], person [8] > person [10]){

				cout << "The person that ate the most pancakes is " << "person 8!" << endl; }

	while (person [9] > person [2],person [9] > person [1],person [9] > person [3],person [9] > person [5], // Checks if person [9] is the largest value in the person array.
			person [9] > person [6],person [9] > person [7],person [9] > person [8],person [9] > person [4], person [9] > person [10]){

		cout << "The person that ate the most pancakes is " << "person 9!" << endl; }

	while (person [10] > person [2],person [10] > person [1],person [10] > person [3],person [10] > person [4], // Checks if person [10] is the largest value in the person array.
			person [10] > person [6],person [10] > person [7],person [10] > person [8],person [10] > person [9], person [10] > person [5]){

		cout << "The person that ate the most pancakes is " << "person 10!" << endl; }


	}



	system("pause");
	return 0;
}
To set to zero elements of an array it is enough to specify empty braces. For example

int person [10] = {};

Or to specify one zero for self documentation of the code

int person [10] = { 0 };

In the both cases all elements of the array will be set to zero.

To enter values of the array it is better to use one loop

1
2
3
4
5
for ( int i = 0; i < 10; i++ )
{
	cout << "Enter how many pancakes person " << i + 1 << " ate." << endl;
	cin >> person[i];
}


The largest element also is searched in a loop. At first you set the largest element to person[0] and then compare it with all other elements of the array. IIf such an element is found value of which is higher than the current largest value you set the current largest value to the value of that element.
The error you get I think is because you are starting to use your array from 1 to 10 where it should be from 0 to 9.

Remember arrays start their index from 0 and end one less of their size, so an array of size 7 would go from array[0] to array[6]
So from what you told me i managed to code this, which is 100% functional. Thanks. However, is there any other way you who are more experienced would do this? Or is this near optimal?

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

int main() {
	
	double hsf = 0.0; // (HighestSoFar)
	int person [10] = {0};
	string pwatm; // Person who ate the most

	for ( int i = 0; i < 10; i++ )
{
	cout << "Enter how many pancakes person " << i + 1 << " ate." << endl;
	cin >> person[i];
}


	do {
		
		if (hsf < person [0])
			hsf = person [0];
			pwatm = "person 1";
		if (hsf < person [1])
			hsf = person [1];
			pwatm = "person 2";
		if (hsf < person [2])
			hsf = person [2];
			pwatm = "person 3";
		if (hsf < person [3])
			hsf = person [3];
			pwatm = "person 4";
		if (hsf < person [4])
			hsf = person [4];
			pwatm = "person 5";
		if (hsf < person [5])
			hsf = person [5];
			pwatm = "person 6";
		if (hsf < person [6])
			hsf = person [6];
			pwatm = "person 7";
		if (hsf < person [7])
			hsf = person [7];
			pwatm = "person 8";
		if (hsf < person [8])
			hsf = person [8];
			pwatm = "person 9";
		if (hsf < person [9])
			hsf = person [9];
			pwatm = "person 10";
	}
	while ( hsf == 0);

	cout << "The person that ate the most pancakes was " << pwatm << " who ate " << hsf << " pancakes!" << endl;

	system("pause");
	return 0;
}
While getting input, keep track of the person who eats the highest pancakes.
And you only need person index...

1
2
3
4
5
6
7
8
9
10
11
12
13
int person [10] = {0};
int hi = 0;  //highest person index

for ( int i = 0; i < 10; i++ )
{
    cout << "Enter how many pancakes person " << i + 1 << " ate." << endl;
    cin >> person[i];
    if (person[i] > person[hi])
        hi = i;
}

cout << "The person that ate the most pancakes was person " << hi + 1
     << " who ate " << person[hi] << " pancakes!" << endl;


The solution you are providing is pretty genius, i'd say.
I would never have thought of that... :)
Topic archived. No new replies allowed.