Returning value AND index in an array?

My task is to find the largest number in an unsorted array recursively. I need to return both the value and in the index. My problem is trying to store the index in a pass by reference parameter. Is this correct?

*EDIT
I've fixed some of the stuff keskiverto was referring to. However it is not printing anything out when i tried printing max.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//finds largest number and its index recursively
int RecursionADT::largest_num(int userArr[], int userSize, int max, int index, int position){
    if(index < userSize ){
        if(max < userArr[index]){
            max = userArr[index];
            position = index;
            index++;
        }
     largest_num(userArr, userSize, max, index, position );
    } 
    
    return max;
}
}
Last edited on
Where is your "by reference parameter"?

Your code doesn't "return" anything. You merely print something to cout, but the caller of this function would surely like to get the values. How about std::pair?

What if I call this function with a large max? Larger than any value in userArray.

The aSize must be either a global or class member. However, the userArray is not. Why would the aSize correspond to the size of userArray? The caller of the function can use "wrong size" array.

Your recursion resembles a loop. You handle one element and then move to next, until at the end. There is a different strategy: split the array in two pieces, recurse to both, and then return the larger result of the two.
Topic archived. No new replies allowed.