Finding the minimum of an array using recursion?

Nt: You're calling findMin recursively, but then not using its return value. What's the relationship between (1) the min of the whole array, (2) the first element, and (3) the min of everything apart from the first element?

So, I should call the findMin method recursively and assign the return value to...? This is where I'm confused. Do I define the return value before calling the findMin method? – user699302 Apr 8 at 22:43.

There are a variety of problems in this code including: You don't use the result of the recursive findMin call. StartIndex will be the same for every call to findMin, because currentIndex is being set to the value of startIndex before startIndex is incremented. If the number at index 1 in the array is.

Also, I'm not understanding how the startIndex is not changing. When I call the recursive findMin method, I'm using the parameters with currentIndex (instead of startIndex), which should then be passed as a new startIndex, correct? I think I'm starting to confuse myself :( – user699302 Apr 8 at 22:39 @user699302: The problem is the currentIndex = startIndex++.

When startIndex is 0, currentIndex becomes 0 and startIndex becomes 1. When you call findMin, you pass it currentIndex, so in that call startIndex will once again be 0, leading to infinite recursion and a stack overflow. – ColinD Apr 9 at 1:27.

Here's a simplified version: public static double min(double elements, int index) { if (index == elements. Length - 1) { return elementsindex; } double val = min(elements, index + 1); if (elementsindex.

A few observations in addition to the first answer: int currentIndex = startIndex++; - you're going to miss your first element here. In general, you don't want to modify the input to your recursive function. Work off the input and generate new values when you're ready to call the function again - i.e.'findMin(numbers, currentIndex+1, endIndex).

The default value of a double doesn't come in to play here, since min is always assigned to a value from the array before it's read. Really, it shouldn't be declared until the point where it's assigned though. – ColinD Apr 8 at 22:14 Whoops, my mistake, misread the locaiton of the min.

Thanks – spinning_plate Apr 8 at 22:15.

I'm trying to find the minimum number in an array using recursion but keep getting an answer of 0.0. My understanding for recursion is that there I need to increment one element and then provide a base case that will end the recursion. I think I'm messing up when I have to return a value and when is best to call the recursion method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions