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
:)

Recommended Answers

All 4 Replies

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

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.

well! I couldn't get your point .Would you please explain more

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.