1

I want to find the Maximum and Minimum element of an array using recursion. Here is the program I've written, I've gone through the logic and its seem to be perfect. But when I compile the program the program get stuck after taking input.

Here's my program:

#include<stdio.h>
#include<conio.h>

int max(int a[], int n);
int min(int a[], int n);

void main(){
    int a[100],n,i,maxi,mini;
    clrscr();
    printf("Enter the number of elements ");
    scanf("%d",&n);
    printf("Enter the elements of array ");
    for(i=0;i<n;i++)
    {
        scanf("%d \n",&a[i]);
    }
    maxi = max(a,n);
    mini = min(a,n);
    printf("\nMaximum element : %d",maxi);
    printf("\nMinimum element : %d",mini);
    getch();
}

int max(int a[],int n){
    int maxo=0,i=0;
    if(i<n){
        if(maxo < a[i]){
           maxo=a[i];
        }
        i++;
        max(a,n);
    }
    return maxo;
}

int min(int a[],int n){
    int mino=999,i=0;
    if(i<n){
        if(mino > a[i]){
            mino=a[i];
        }
        i++;
        min(a,n);
    }
    return mino;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Danish Shah
  • 13
  • 1
  • 3
  • 2
    That's a rather curious loop/recusrion hybrid. If you want to recurse, you should recurse on arrays of ever decreasing size and have a base case where an array has only one element. Otherwise you'll recurse forever. – M Oehm Aug 25 '15 at 18:25
  • Minor: Suggest `#include ` `maxo=INT_MIN` and `int mino=INT_MAX` – chux - Reinstate Monica Aug 25 '15 at 18:25
  • did the program get 'stuck' during the compile step or during the execution step? the question seems a bit vague on that detail. – user3629249 Aug 25 '15 at 18:47
  • the header file: conio.h is not portable (not available outside of visual studio/windows) Strongly suggest not using that header file. There are better system functions available in the standard C librarys – user3629249 Aug 25 '15 at 18:49
  • @user3629249 OP posted "But when I compile the program the program get stuck after taking input." Sounds like a run-time issue to me. – chux - Reinstate Monica Aug 25 '15 at 18:50

2 Answers2

5

Your functions max and min cause infinite recursion. That leads to stack overflow.

You have:

int max(int a[],int n){
   int maxo=0,i=0;
   if(i<n){
      if(maxo < a[i]){
         maxo=a[i];
      }
      i++;
      max(a,n);
   }
   return maxo;
}

The trouble with using recursive approach is that i is initialized to 0 in every recursive call. The fact that you are using i++ does not affect the value of i in the next recursive call. Similarly, the value of maxo is set to 0 in every recursive call.

To make the recursive function to work, you'll need to pass i and maxo in the recursive function calls. Something like:

int max(int a[],int n, int i, int maxo){
   if(i<n){
      if(maxo < a[i]){
         maxo=a[i];
      }
      return max(a, n, i+1, maxo);
   }
   return maxo;
}

and call the function with:

maxi = max(a, n, 0, a[0]);

Make similar changes to the definition of min and the call to min.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

Code recurses indefinitely

2 changes made to OP's code

#include <limits.h>

int max(int a[],int n){
    //int maxo=0,i=0;
    int maxo=INT_MIN,i=0;  // Initialize maxo to the minimum int
    if(i<n){
        if(maxo < a[i]){
           maxo=a[i];
        }
        i++;
        // max(a,n);  
        // go to next element in the array, decrement array size. 
        // Without eventually reducing the array size, code recurses indefinitely.
        // So here max() works on a 1 smaller array.
        int m = max(a+1,n-1);  
        // save the greater one
        if (m > maxo) maxo = m;
    }
    return maxo;
}

Using recursion when a loop is better gives recursion a bad name. Consider the following which halves the array size on each call. In the end there are still about n calls to max(), but the recursion depth is only log2(n) rather than n.

int find_max(const int *a, size_t n) {
  if (n <= 1) {
    return (n == 0) ? INT_MIN : a[0];
  }
  int left = find_max(a, n/2);
  int right = find_max(&a[n/2], n - n/2);
  return left > right ? left : right;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256