Largest Element in an Array.....

Hi I'm currently doing a group of exercises from a previous post from this forum.

Pancake Glutton
----------------

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 which person ate the most pancakes for breakfast.

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

I am trying to display the largest element in an array of 10 int's without getting into the whole business of sort algorithms yet.. Below is code based on help I got from another user on this forum
which really helped.
I just wanted to ask why I get random garbage numbers after the user has inputted all the numbers into the array. When I initialise the array with curly braces it works fine. But If the user inputs numbers via for loop, at the end the console just spits out a bunch of random numbers ???

Also I know that I could be (or should be) using a MAX function for this... I'm still learning... Thanks a lot for your help and patience.

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

#include "stdafx.h"
#include <iostream>

using namespace std;


int main(array<System::String ^> ^args)
{

															
		const int m = 3;   // for the loops test condition					

		int    v[3];	  // array 					
											

		int max = v[0];						
											

		int i;								

		for (i=0; i<m; i++)	// array initialisation with for loop
		{
			cout << "Enter" << endl;
			cin >> v[i];					
											
			{
			if (max < v[i])					 
			max=v[i];						
		}

}


// What the conditional statement should be doing is......

// 1. grab the first element
// 2. check the next value, if it's greater, grab that value replacing the one // you have
// 3. repeat 2, until you hit the end of the array... 






I don't see where your program outputs the result but do you need the max amount of pancakes someone at or the person who ate the most? In other words, are you expecting a value stored in the array or an index of the highest value?
The code you got has the basic idea covered but seems like you are getting the highest value (max) and not an index. Is that what you meant to do?
There seems to be an extra curly brace at line 26.
I think the problem is this: int max = v[0];
This initializes max with v[0], but you haven't initialized v[] yet, so it will have a random value from the memory. You could test this by putting a cout << max there, to see what value max gets first.

Also, as Zeillinger said, at line 28 you should have max = i+1, because you want to know which person ate the most pancakes. You should have i+1, not i, because your array's first index is 0, but the first person is, I assume, person 1.
Thanks for the replies......

@Zeillinger, yes I want the max amount of pancakes eaten (the largest element in the array to be displayed)........

Also I didn't copy and paste that section of code, sorry for any confusion caused....

@cristi121 I think your right I will try what you said...

Thank you both so much
@cristi121 I just tried what you said and it actually outputs 0 to the console screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include <iostream>

using namespace std;

int main(array<System::String ^> ^args)
{

	//cout << "Hello World" << endl;			      // Test line


		const int m = 3;					// This is the definition of varaible m, its a constant int set to 3.

		int      v[3];						// This is the defination of the array v. It is of type int and 3 elements
											// in size	

		int max = v[0];						// This is the variable max (Which I am assuming its purpose is to display 
											// the maximum value of array v) Its value has been set to first element array v[]

		cout << max << endl;


It's odd that it doesn't work.

This works for me:

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
#include <iostream>

using namespace std;


int main()
{

		const int m = 3;   // for the loops test condition

		int v[3];	  // array

		int max = 0;       //doesn't work if i put v[0], it outputs a random number

		for (int i=0; i<m; i++)	// you can declare i in for
		{
			cout << "Enter "; 
			cin >> v[i];
                                          cout << endl;

   			if (max < v[i])
			max=i+1;    
		}

         cout << "Person " << max << " really likes pancakes!";
}
Thank you so much..... I will try this right now
@crisit121 thanks a lot it worked really well
Topic archived. No new replies allowed.