Output Max

Hi, i did this program and now i want it to output the biggest value, someone can help me?
And please explain what you did, i want to know how to do.
Thanks in advance,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main ()
{
	int x,n,pancakes[10];
	for (n=0;n<10;n++)
	{
		cout << "Enter the amount of pancakes that person " << n << " ate: ";
		cin >> pancakes[n];
	}
	getchar();
	cout << "Who do you want to know how much pancakes ate?\nPerson: ";
	cin >> x
	getchar();
	cout << "Person " << x << " ate " << pancakes[x];
	getchar();
	cout << "The person who ate most pancakes is ";
        getchar();
	return 0;
}
try this

1
2
3
4
5
6
7
8
9
10
11
12
int largest = 0;


for (int i = 0; i<10;i++)
{

if (pancakes[i] > largest)
{
largest = pancakes[i];
}

}
Can you explain what you did?
1
2
3
4
5
6
7
8
9
10
11
12
int largest = 0;//declare a variable to hold the largest number and set it to 0


for (int i = 0; i<10;i++)
{

if (pancakes[i] > largest)//if the first number in the array is larger than 'largest' make it the largest number
{
largest = pancakes[i]; //so every time a largest number is found in the array that number is sent to the 'largest' variable
}

}


that way the 'largest' variable will contain the largest number in the array once the for loop is done

hope that helps
Last edited on
:D thank you so much.
i'll see what i can do on my program :D
And if i want to know the person and not the amount?
What can i do to localize the person that have the larger value?
Topic archived. No new replies allowed.