### nputting values into an array, then finding the largest value in the array. my function is returning a random number

Hello all, I appreciate you taking the time to look at this. Basically what I need to do for this is to input up to 100 integers into an array and stop when the user enters 0. Then I need to find the largest value the user entered into that array. I have the code to input values into the array and stop when the user enters 0 working just fine, and it is as follows:

 ``12345678910111213141516171819202122`` `````` 5 #include 6 using namespace std; 7 8 9 10 11 int main() 12 { 13 const int MAX = 100; 14 int values[MAX]; 15 int count; 16 17 for(int i = 0 ; i < MAX; i++) 18 { 19 count++; 20 cin >> values[i]; 21 22 if(values[i] == 0) 23 break; 24 } 25 } ``````

That is working fine, and after that I need to find the largest value the user input into the array, so this is my whole program so far:
 ``1234567891011121314151617181920212223242526272829303132333435363738`` `````` 5 #include 6 using namespace std; 7 8 int find_largest(int values[], int size); 9 10 11 int main() 12 { 13 const int MAX = 100; 14 int values[MAX]; 15 int count; 16 17 for(int i = 0 ; i < MAX; i++) 18 { 19 count++; 20 cin >> values[i]; 21 22 if(values[i] == 0) 23 break; 24 } 25 26 cout << "Max value is: " << find_largest(values, MAX) << endl; 27 28 } 29 30 int find_largest(int values[], int size) 31 { 32 int largest = values[0]; 33 for(int n = 0; n < size; n++) 34 { 35 if(values[n] > largest) 36 largest = values[n]; 37 } 38 return largest; 39 40 41 } ``````

for some reason, when I try to output the find_largest function, it is giving me a large, random number. I created a seperate file to test out the function to find the largest number in an array, and i have that working just fine aswell, it is as follows:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````1 #include 2 using namespace std; 3 4 int find_largest(int values[], int array_size); 5 6 int main() 7 { 8 const int MAX =8; 9 int array[MAX]; 10 11 array[0] = 56; 12 array[1] = 23; 13 array[2] = 69; 14 array[3] = 127; 15 array[4] = 101; 16 array[5] = 99; 17 array[6] = 1; 18 array[7] = 27; 19 20 cout << "Max is: " << find_largest(array, MAX) << endl; 21 22 } 23 24 int find_largest(int array[], int array_size) 25 { 26 int largest = array[0]; 27 for(int i = 0; i < array_size; i++) 28 { 29 if(array[i] > largest) 30 largest = array[i]; 31 } 32 return largest; 33 } 34 ``````

This function finds the largest value in the array with no problems, and this is the same function that I have in my main program. Why is this function returning the largest number in the array and the one in my main program returning a random number?
You did not initialize variable count

 ``12345678910`` `````` int count; for(int i = 0 ; i < MAX; i++) { count++; cin >> values[i]; if(values[i] == 0) break; }``````

It would be much simpler to write

 ``123`` `````` int count = 0; while ( count < MAX && cin >> values[count] && values[count] != 0 ) count++;``````

 Basically what I need to do for this is to input up to 100 integers into an array and stop when the user enters 0.

The function compares the initial element to all of the elements within the array (if the second argument is the array's size). That is a problem if not all of the elements within the array have been assigned a value. If they have not been, the function is comparing the initial element to garbage values, which just happen to be larger than the values entered. That is where the 'random' value is coming from.

Initialize the count variable to zero and then pass it to the 'find_largest' function as the second argument to fix the problem.
Last edited on
Awesome, I should have seen that. Thank your for the input, it is greatly appreciated.
Topic archived. No new replies allowed.