0

I'm doing a function that returns the index of the largest integer in the array it's an easy function.I've done it but I don't know why the output is always 10 although i want to return only the index of the largest number and here is my code:

int MAX_INT(int arr[],int s)
{int temp;
    for(int Index=0;Index<s;Index++)
    {
        if(arr[Index]<arr[Index+1])
        temp =Index;
        else 
            temp= Index+1;

    }
    return temp;
}
int main()
{
   const int SIZE=10;
int arr[SIZE];
    for(int i=0;i<SIZE;i++)

        cin>>arr[i];


    cout<<MAX_INT(arr,SIZE);
}

Thank you
:)

Edited by mike_2000_17: Fixed formatting

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by reemhatim
0

look here temp= Index+1;
shouldnt you be assigning a value from the array and not just a counter

0

You're making life difficult on yourself:
Define a variable max, set it equal to the first element of the array
Step through the array, if the current element in the array is greater than max, let max be equal to that element, and set temp = index. Only change the index if you find a new max.

0

well ! I didn't see your reply jonsca before posting ,Thank you very much that really helps :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.