0

I want to find the max number in array by recursion, What wrong with this code.

 #include<stdio.h>
int find_max(int *a,int n){
    int m_max;
    if(n==1){
        return a[0];
    }
    m_max=find_max(a,n-1);
    if(a[n-1]>m_max)
        m_max=a[n-1];
}
1
  • 1
    There isn't a return when n != 1 Commented Mar 20, 2015 at 12:57

4 Answers 4

3
  1. As @moffeltje comments, "There isn't a return when n != 1"

      if(a[n-1]>m_max) 
        m_max=a[n-1];
      return m_max;  // add
    }
    
  2. This linear approach gives recursion a bad name. If there are n numbers, the maximum recursive depth is n. Much better to divide in 2 for a max depth of log2(n)

    int find_max(const int *a, int n) {
      if (n <= 1) {
        return a[0];
      }
      int left = find_max(a, n/2);
      int right = find_max(&a[n/2], n - n/2);
      return left > right ? left : right;
    }
    

--- Minor stuff follows

  1. Cope with corner cases where n < 1 or a == NULL.

    int find_max(const int *a, int n) {
      if (n <= 1 || a == NULL) {
        if (n <= 0 || a == NULL) return INT_MIN;  // or throw error
        return a[0];
      }
      ...
    
  2. Changing int find_max(int *a, int n) --> int find_max(const int *a, int n) allows constant arrays to be passed also.

  3. Array sizes are best typed as size_t rather than int.

    int find_max(const int *a, size_t n) {
      ...
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1
#include<stdio.h>
int find_max(int *a,int n){
    int m_max;
    if(n==1){
        return a[0];
    }
    m_max=find_max(a,n-1);
    if(a[n-1]>m_max)
        m_max=a[n-1];
    return m_max;
}

you have not returned anything when n!=1. Look when designing these things you should always check the -

The base case. Here in this case it is achieved when n=1.(one element left and it is the largest one)

The recursive case. Here you will use the computed values to smaller cases and now you will build the solution thinking that the previous cases are calcualted already.

Combine Combine the calculate results. The last if condition that you have provided. But you forgot to put a return that is not giving the result to one stage to others. That's where there is this problem.

2 Comments

there is another way to do this?
@user3234880.: Using recursion you can do it this way. But simply using a variable you can also do this..int max=a[0]; for(i=1;i<n;i++) if(a[i]>max) max=a[i];
0

Alternatively, you could simplify the function.

int max_array_val( int* a_Array, int a_Size )
{
    int max = a_Array[ 0 ];

    for( int i = 1;  i < a_Size;  i++ )
        if( a_Array[ i ] > max )
            max  = a_Array[ i ];

    return max;
}

int main()
{
    int int_array[ 6 ] = { 3, 2, 6, 5, 5, 2 };

    printf( "Maximum number is: %i\n\n", max_array_val( int_array, sizeof( int_array ) / sizeof( int ) ) );
    system( "pause" );

    return 0;
}

1 Comment

It has to be with a recursion, if I understood correctly.
0
int getMax(int const * arr, int size) {
    if (size == 1)
        return *arr;

    int ans = getMax(arr + 1, size - 1);
    return *arr > ans ? *arr : ans;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.