# highest int

• 08-30-2010
dhuan
highest int
I've been trying to make this code work for hours, but it's really messing my head up...
Its objective is simple, the user chooses how many numbers he wants to input, then the program output the highest.

Code:

```#include <iostream> #include <iomanip> using namespace std; int returnHighest(int*,int); int main() {     int inputX;     cout<<"How many numbers do you wanna input? ";     cin>>inputX;     int listOfNumbers[inputX];     int n = 1;     while(n <= inputX)     {         int inputY;         cout<<"Input N. "<<n<<": ";         cin>>inputY;         listOfNumbers[n] = inputY;         n++;     }     cout<<"The highest number is: "<<returnHighest(listOfNumbers,inputX); } int returnHighest(int * list,int arraySize) { int returnValue; //the following FOR loop will //search for the highest int //stored in "list" int array int num = 0; for(int x = 0 ; x <= arraySize; x++) {     if(list[x] > num)     {         num = list[x];     } } returnValue = num; return returnValue; }```
• 08-30-2010
Salem
Your first loop starts at 1, and finishes 1 place past the end of the array.

Your second loop starts at 0, and finishes 1 place past the end of the array.

Try a
for ( i = 0 ; i < n ; i++ )
kind of loop in both places.
• 08-31-2010
Elysia
Aside from that Salem mentioned:

>>int listOfNumbers[inputX];
This is not ever going to work. Ever.
But that doesn't mean it can't be done easily. You use a vector:

#include <vector>
std::vector<int> ListOfNumbers(InputX);

And, of course, the vector keeps track of its size, so you can do:

ReturnHighest(ListOfNumbers,ListOfNumbers.size());

Or even integrate that into the loop:

for(int x = 0 ; x < list.size(); x++)

And btw, 0 is not guaranteed to be the lowest possible number. Consider what happens if the user enters negative numbers.