12,064,291 members (47,348 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to find the max number in the array using recursion, but it keeps returning the index at 0.

I was wondering if this is the right way to go about doing it:
```public static double findMax(double[] numbers, int count){
double max = numbers[0];
double current= numbers[count-1];

if(count-1==0){
return max;
}
else{

if(current>max){
max= current;
}

return  findMax(numbers,count-1);```
Posted 28-Mar-13 10:02am
Edited 28-Mar-13 10:16am
v4
Comments
Sergey Alexandrovich Kryukov 28-Mar-13 15:05pm

What, could not see what's going on under the debugger? And why using recursion at all?
—SA
diego14567 28-Mar-13 15:08pm

the problem states we must use recursion, but i cant seem to get it to give the max output as it always returns the index at 0
Sergey Alexandrovich Kryukov 28-Mar-13 15:18pm

Who told you that you "must"?

Event with recursion, this is a pretty easy exercise, but with no practical sense and is not efficient. If it is your assignment at programming class, you really need to solve the problem by yourself.
You should remember that in Java variables are passed by value.
—SA
diego14567 28-Mar-13 16:31pm

my computer science told us we must use recursion or else it will not be counted and we cannot use loops to find the values either

## 5 solutions

Rate this:
Please Sign up or sign in to vote.

## Solution 2

Hint #1:
Since you are calling `findMax` recursively as the last thing in the function, this is "tail recursion". And tail recursion can be replaced with a loop around the entire function body. If you do this it should become more apparent what is wrong. (I know you said you must use recursion. This is just an exercise in analyzing the behavior of your code.)
```public static double findMax(double[] numbers, int count){
do
{
double max = numbers[0];
double current= numbers[count-1];

if (count-1 == 0) {  // why not if (count == 1) ??
return max;
}
else{

if (current > max){
max = current;
}

// return  findMax(numbers,count-1);
count = count - 1;
}
} while (true);
}
```
Hint #2: What is happening to the variable `max` each time?
v2
Comments
Sergey Alexandrovich Kryukov 28-Mar-13 15:33pm

Some food for thought, right. My 5.
—SA
Rate this:
Please Sign up or sign in to vote.

## Solution 4

If you look at your code, you are never returning max, you are returning the result of your recursion unconditionally... and as the item zero is the last one verified, it will return such value.

I am not testing it, so it may miss something, but a correction could be:

```public static double findMax(double[] numbers, int count){
if (count <= 0)
throw new ArgumentOutOfRangeException("count");

double current = numbers[count-1];

if(count==1){
return current;
}
else{

double recursiveMax = findMax(numbers,count-1)
if(recursiveMax>current){
return recursiveMax;
}

return current;
}
}
```
v3
Comments
CPallini 28-Mar-13 16:46pm

Good, my 5. However Java arrays are 0-based.
Paulo Zemek 28-Mar-13 17:42pm

It was an error really... it should be double current=numbers[count-1]... but I said I didn't test.
CPallini 29-Mar-13 17:01pm

Well, you might fix it.
Paulo Zemek 29-Mar-13 17:16pm

I think it is corrected now... and with a validation at the beginning to make it better.
Rate this:
Please Sign up or sign in to vote.

## Solution 5

```public static double findMax(double[] numbers, int count)
{
double c = numbers[count-1];
if (count==1)
return c;
else
{
double f = findMax(numbers, count-1);
return c > f ? c : f;
}
}```
Comments
Sergey Alexandrovich Kryukov 28-Mar-13 17:05pm

Well, quite compact. It's nice that it uses only two parameters. My 5.
But too bad OP does not deserve it. I still think it's the best not to provide complete answers to homework questions.
—SA
CPallini 29-Mar-13 17:00pm

Thank you, Sergey.
Rate this:
Please Sign up or sign in to vote.

## Solution 6

i was able to solve it myself heres the code thanks for the help anyways
public static double findMax(double[] numbers, int count){
if(count <= 0){
return Integer.MIN_VALUE;
}

double max = numbers[0];

if(count-1==0){
return max;
}
else{
double current= numbers[count-1];
if(current>max){
max= current;
}

numbers = Arrays.copyOfRange(numbers, 1, count - 1);
double max2 = findMax(numbers,count-2);

if(max2> max){
return max2;

}
else{
return max;
}
}

}
Rate this:
Please Sign up or sign in to vote.

## Solution 1

Please see my comment to the question.

First of all, using recursion for solving such problem is a bad idea.

You algorithm is not working because it's wrong in principle, the whole idea is wrong. You don't have an object storing current maximum value. The variable `max` is created on stack each time; and it is always the same value: your original array value at index 0. Also, it's apparent that you need the comparison with maximum value. But you have only one comparison; and this comparison does not modify anything: you just modify the value of `max` which is no longer used; it is removed from stack and simply disappears. It looks like you have no idea how stack variables are allocated and used, that's the problem.

That should be more than enough to explain why your algorithm does not find the maximum. I don't think I should bother about correct algorithm: loop based algorithm is too trivial, and the recursive one makes no sense.

—SA
Comments
CPallini 28-Mar-13 16:47pm

It could be homework.
Sergey Alexandrovich Kryukov 28-Mar-13 16:57pm

According to the last OP's comment, it is; and that's why I don't want to post a solution...
—SA
H.Brydon 29-Mar-13 22:18pm

OP flagged java as language of interest, but it turns out that with functional languages and/or functional algorithms with non-functional languages, recursion can be used to solve a lot of problems such as this. IMHO we will see a lot more of this in coming years especially with parallel algorithms. I also recently briefly studied a functional dialect of java that require you to do this sort of thing...

[no arguments with your solution.]
Sergey Alexandrovich Kryukov 29-Mar-13 22:40pm

Thank you for you interesting note.

[My solution was based on the idea that OP just wanted to understand why his code does not find the maximum...]
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 d@nish 200 CPallini 165 CHill60 120 OriginalGriff 74 BillWoodruff 55
 Dave Kreskowiak 1,827 Richard MacCutchan 1,250 d@nish 980 OriginalGriff 874 CPallini 740

Advertise | Privacy | Mobile
Web02 | 2.8.160204.4 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100