views:

159

answers:

8
#include <iostream>
using namespace std;

int recursiveMinimum(int [], int n);

int main () 
{
    int theArray[3] = {1,2,3};

    cout << recursiveMinimum(theArray, 0);
    cout << endl;
    return 0;
}


// pass in array and 0 to indicate first element
// returns smallest number in an array
int recursiveMinimum (int anArray[], int n) // nth element is smallest element in anArray
{
    while (anArray[n+1] != NULL)
    {
     int smallest = n;
     if (anArray[n+1] <= anArray[n])
      smallest = n + 1;
     //if last element has not been reached
     return recursiveMinimum(anArray, smallest);
    }
}

My function exits, but it doesn't return anything. I tried to set the base case to when the outside of the array is reached. The return 0 line in main is reached so I'm pretty sure the base case in my function is being reached.

Here is the working function:

#include <iostream>
using namespace std;

 int recursiveMinimum(int a[],int min,int index,int size);

int main()
{
    int a[6] = {8, 2, 55, 3, 11, 9};
    cout << recursiveMinimum(a,a[0],1,6) << endl;
    return 0;
}

// pass in the array, the first element, 
// 1 to indicate 2nd element, and the number of elements
int recursiveMinimum(int a[],int min,int i,int size)
{
    if(i == size )
       return min;

    else if(i < size)
    {
       if(a[i] < min)    
          recursiveMinimum(a,a[i], i + 1, size);
       else 
          recursiveMinimum(a,min, i + 1, size);      
    }

}

Thank you to everyone who helped. Due to time constraints I sent out a SOS (Stack Overflow distress Signal), however next time I will definitely step through the debugger prior to asking a question.

+2  A: 

You need to have an end case with a recursive function.

At the moment, your function always returns itself. This will recurse until the stack runs out. You need to have a check that says "I'm done", which will return a value rather than the result of another function call.

Skilldrick
it's simple to get around 'end cases' in recursion by having recursive methods run out of data to work with
Hardryv
Yes, but they have to stop calling themselves!
Skilldrick
+1  A: 

Because your while loop never terminates. Why are you sure anArray[n+1] will ever be NULL?

ctford
Let me make the point explicitly. Only strings are null-terminated. An array created with curly braces will not have a null character at the end. The way around this is to explicitly put a number at the end to signify the end of the array: {1, 2, 3, -1}, or pass the length of the array as an argument to the recursiveMinimum function.
Brian
Ok thanks for the tip.
Brandon
+1  A: 

You never break your recursion. Actually I wonder that this compiles as your function doesn't even have a defined return value in case it reaches the end. Also using while there seems unnecessary as the function execution stops after the return anyway.

Joey
+4  A: 

Have you stepped through this in a debugger? It should become fairly obvious when you do.

If he's taking a class like the one I used to help teach, he hasn't learned about debuggers, and he probably doesn't have time to learn how to use one before the assignment is due at the end of the semester, either.
Rob Kennedy
That may be the case if he's using Linux/GCC, but most likely he has Visual Studio installed. What can be harder than setting a break-point and tapping F10/F11? Also, installing Visual C++ Express Edition takes like 5 minutes? In addition to that, I'm sure the OP can get the full blown VS2008 at reduced educational cost from his/her school! If they are really using Linux GCC, it takes like 5 minutes to Google a simple tutorial on the web! There simply are no excuses!
+3  A: 

You are recursing within the while loop:

while( condition ) recursive call while( condition ) recursive call . . .

Instead what you probably were thinking was

if( condition ) recursive call recursive call recursive call

you see? Get rid of the while and replace it with an "if" statement.

wheaties
The condition is bad to begin with. `n + 1` is not necessarily a valid index into the array. And checking for `NULL`? What is that accomplishing?
Jason
You are correct sir in that it is a bad exit condition. However, I think the larger issue with the proposed algorithm was the use of a recursive call within a while loop.
wheaties
I was trying to check for the outside of the array using NULL.
Brandon
Brandon, once you have something working mind posting it up here? I bet you could elicit more commentary. Howver, Jason is correct that NULL is not a good condition to check for the end of an array. There's no guarantee NULL will be found! People generally pass along the size of the array in C functions. While you often can do '/0' for strings I don't think the same can be said for an arbitrary array.
wheaties
Ok no problem. Its going to take me a bit because I am still not sure how to fix it, but when I do I will update for sure. Thanks!
Brandon
+1  A: 

Instead of

int recursiveMinimum(int array[], int n);

I think that recursiveMinimum should be defined as

int recursiveMinimum(int array[], int index, int length);

with the intention that recursiveMinimum will return the minimum value in array between indexes index and length (i.e., min array[i] where i in [index, length)). Of course, you want to define this recursively. So then I would note that the minimum value in array between indexes index and length is

min(array[index], recursiveMinimum(array, index + 1, length));

Of course, there are boundary cases (such as when index = length - 1). In this case you would just return array[index] as the minimum.

Hope this helps. Let me know if this does not make sense.

Jason
+1  A: 
 int recursiveMinimum(int a[],int min,int index,int size)
{
    if(index == size )
       return min;

    else if(i < size)
    {
       if(a[i] < min)    
          recursiveMinimum(a,a[i],++index,size);
       else 
          recursiveMinimum(a,min,++index,size);      
    }

}

int main()
{
    int a[] = {1,2,3,0,-4};
    int min = recursiveMinimum(a,a[0],1,5));
    return 0;
}

When you use recursion make sure that you must put some exit condition to end it ,otherwise you will have in infinite recursion and you program will hang.

I think finding minimum is more efficient,easy and simple using iteration rather than recursion.

Ashish
Thanks for taking the time to code this. I modified it a bit and it worked like a charm.
Brandon
Brandon it took hardly 45 sec just to write it.
Ashish
+1  A: 

You're misunderstanding the point of a recursive function. If a certain condition is true, then you return the result of a call to the recursive function, otherwise you return some other value. For example, here is a recursive definition of the factorial function (e.g. 5!, or "5 factorial", is 5*4*3*2*1, which is 125):

int
factorial (int n)
{
  if (n == 1)
    return n;
  return (n * factorial (n - 1));
}

How it works:

  • If n is 1, then return 1 since 1! is 1.

  • Otherwise, return n multiplied by one less than n.

For example, if n is 5, then the return value is the result of the expression 5 * factorial (5 - 1), which is the same as 5 * factorial (4). Of course, the same thing happens again since it's a recursive function and 4 is not 1. So you end up with 5 * factorial (4), which is the same as 5 * (4 * factorial (4 - 1)), or 5 * (4 * factorial (3)).

You should be able to see the pattern now and how the factorial function works. Your recursiveMinimum function should adhere to the same general idea -- if something is true (or not true), return the result of a call the function (possibly with some additional things like the value n inside the factorial function needs to be multiplied by the result of the factorial function), else return a certain value and code the function to handle it appropriately. In other words, you need a "final" exit condition, such as the n == 1 condition in the factorial function above.

Good luck!

Dustin