finding the highest number

i am trying to make a program that asks the user to type 10 integers and writes the highest value but i can just write 10 integers. how can i do this ? please, i need help
closed account (zb0S216C)
So you want to find the largest number out of 10 integers, eh? It's relatively straightforward. This example does such an operation:

1
2
3
4
5
6
7
8
9
10
int FindLargest( const int( &IntArray )[10] )
{
    int Largest( IntArray[0] );

    for( short i( 0 ); i < 10; i++ )
        if( Largest < IntArray[i] )
            Largest = IntArray[i];

    return Largest;
}

This code is simple to understand. The single parameter is a reference to an array of 10 integers. The parentheses enclosing &IntArray are required, otherwise, the compiler will think it's an array of 10 references to integers. The local variable, Largest, stores the largest number within IntArray. Its functionality becomes obvious within the for loop construct. Next, a for loop is entered, which loops 10 times. The if statement is used to determine if the value stored within Largest is less than the ith value within IntArray. If it is, Largest then obtains the ith value of IntArray.

Wazzak
Last edited on
If the user is entering these values during runtime you simply store two numbers.
int highest, user;
Set highest to the first input automatically. Then simply loop comparing the users input with highest in an if statement. If the user is greater than the current highest, set highest to the user.
Once all ten numbers are entered, simply print the value of highest.
#include<iostream>
using namespace std;
int main()
{

int FindLargest( const int( &IntArray )[10] ){

int Largest( IntArray[0] );

for( short i( 0 ); i < 10; i++ )
if( Largest < IntArray[i] ){
Largest = IntArray[i];
}
return Largest;
}

still theres an error.
closed account (zwA4jE8b)
to pass an array to a function the syntax is

1
2
3
4
int FindLargest(int A[])
{
....
}


try that.

look at the bottom of this page
http://cplusplus.com/doc/tutorial/arrays/
Last edited on
closed account (zb0S216C)
enaideam wrote:
still theres an error. (sic)

Can you be more specific? It works on my compiler (no errors or warnings). Also, what compiler are you using?

Wazzak
Last edited on
i am using dev c++
closed account (zb0S216C)
Your compiler and IDE is very old. Chances are, it doesn't support references to arrays. You need a more up-to-date IDE such as Microsoft Visual C++ 2010/2008, or Code::Blocks.

Wazzak
Topic archived. No new replies allowed.