-5

Here is the program which I have coded. I created a function "getMax" which returns the value of the maxima in an array. Then using this value I applied a for loop to find out the index of this maxima.

int main(){
    int arr[10];
    int Imax = 0;
    cout << "enter the elements of the array of sise 10" << endl;
    for (int j=0; j<10; j++)
        cin >> arr[j];
    int getMax(arr[10]);

    for(int k=0; k<10; k++) {
        if(arr[k]== getMax)
            Imax+=1;
    }

    cout << "the index of the max element is" << Imax;

    return 0;
}


int getMax(int arr[]) {
    int max= 0;
    for(int i=0; i<10; i++){
        if (arr[i]>max)
        max=arr[i];
        return max;
    }
}
Sonu Gupta
  • 45
  • 7
  • 2
    How many times will the loop in `getMax` iterate before hitting the `return`? – Stephen Newell Oct 18 '22 at 20:53
  • The first thing I see that's wrong is the lack of `#include `. The next ting I see wrong is `cin` is not fully qualified. It should be `std::cin`. The third thing is `int getMax(arr[10]);`, which looks like part function declaration and part function call (with an out-of-range array value). I don't think this code compiles. – user4581301 Oct 18 '22 at 20:56
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 18 '22 at 21:06
  • What do you think this statement does? `if (arr[k] == getMax)`? The `getMax` is a function and without `()`, represents the location or address of the function. There should be a warning or error for this. – Thomas Matthews Oct 18 '22 at 21:10

1 Answers1

2

If you're asking what's wrong here, there's a bunch. I'll point out what I see

int main(){
    int arr[10];
    int Imax = 0;
    cout << "enter the elements of the array of sise 10" << endl;
    for (int j=0; j<10; j++)
        cin >> arr[j];
    int getMax(arr[10]); //This isn't how you call a function. Should be
                         //int max = getMax(arr);

    for(int k=0; k<10; k++) {
        if(arr[k]== getMax) //Should be arr[k] == max;
            Imax+=1;    //Should be Imax = k;
    }

    cout << "the index of the max element is" << Imax;

    return 0;
}


int getMax(int arr[]) {
    int max= 0;
    for(int i=0; i<10; i++){
        if (arr[i]>max)
        max=arr[i];
        return max; //This will just return after one loop of the for loop.
    }
    //return max; should be here
}

All this plus what user4581301 put in their comment.