Home > Mobile >  Function & array [Getting Max Number]
Function & array [Getting Max Number]

Time:11-25

#include <iostream>

using namespace std;


int getMax(numbers[], int size){
    int max = numbers[0];
    for(i=1; i<size; i  ){
        if(numbers[i] > max)
        max = numbers[i];
    }
    return max;
}


int main(){
    
    int numbers[6] = {31,23,45,6,7,-2};
    cout << getMax(numbers, 6) << endl;
    
    
    return 0;
}

I can't seem to get the max number that I wanted from this code. And this is also my first time using stack overflow, so if there isn't enough information. Please spare me :)

I am not sure if tried enough soultions to slove this problem. I just wanted to see how stack overflow worked and whether I will get my question answered. :)

CodePudding user response:

The only thing I see wrong with this program are syntax errors. Compiling yields this:

error: 'numbers' was not declared in this scope
    6 | int getMax(numbers[], int size){
      |            ^~~~~~~

That's because you forgot to specify the type of the variable numbers. It is an integer array. You can fix this by writing:

int getMax(int numbers[], int size)

Similarly, you forgot to define i in the loop. Write this:

for(int i=1; i<size; i  )

Get in the habit of reading the compiler's error messages. They are important, and they are trying to tell you what's wrong. Always read the first error. Subsequent errors may be confusing, due to the flow-on effect of earlier errors.

  • Related