• Create Account

### #ActualCuajin

Posted 18 April 2012 - 04:30 PM

Hi. I understand recursion is not good for this sort of operation, but I am trying to learn.

I want to find the position of the largest int in an array recursively.

myList = {5, 2, 3 , 1, 22, 4, 9} ;

public int findIndex(int[] myList, int size)
{
if (size == 1)
return size-1;

return myList[size-1] > findIndex(myList, size-1) ? size-1 : size ;
}


My code produces this output: position 6. Which is wrong.

How do I do this?

### #2Cuajin

Posted 18 April 2012 - 04:29 PM

Hi. I understand recursion is not good for this sort of operation, but I am trying to learn.

I want to find the position of the largest int in an array recursively.

myList = {5, 2, 3 , 1, 22, 4, 9} ;

public int findIndex(int[] myList, int size)
{
if (size == 1)
{
return size-1;
}
return myList[size-1] > findIndex(myList, size-1) ? size-1 : size ;
}


My code produces this output: position 6. Which is wrong.

How do I do this?

### #1Cuajin

Posted 18 April 2012 - 04:28 PM

Hi. I understand recursion is not good for this sort of operation, but I am trying to learn.

I want to find the position of the largest int in an array recursively.

myList = {5, 2, 3 , 1, 22, 4, 9} ;

public int findIndex(int[] myList, int size)
{
if (size == 1)
{
return size-1;
}
return myList[size-1] > findIndex(myList, size-1) ? size-1 : size ;
}


My code produces this output: position 6.

How do I do this?

PARTNERS