0
#include<stdio.h>
#define SIZE 7

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

int main(void) {
    int a[ SIZE ] = { 5, 7, 4, 3, 5, 1, 3 }; // Number 2 is not initialized.

    printf( "The smallest number is %d", recursiveMinimum( a, SIZE ) );

    return 0;
}

int recursiveMinimum( int a[], int size ) {
    static int min ;
    static int i = 0;

    min = a[ i ];
    if( a[ i + 1 ] < min ) {
        min = a[ i + 1 ];
    }

    i++;

    if( i == size  ) {
        return min;
    } else {
       return recursiveMinimum( a, size  );
    }
}

So why does it print 2?

3
  • 1
    I suspect a typical case of "artificial example only to learn recursion". The only use for it is the day when you become a teacher, then you can teach others how to use recursion with artificial examples, so that the day when they become teachers they can teach others how to use recursion with artificial examples, so that-... Commented Apr 10, 2015 at 13:27
  • possible duplicate of Find the minimum number in an array with recursion? Commented Apr 10, 2015 at 13:30
  • There is no need for static variables in recursiveMinimum(). Commented Apr 10, 2015 at 13:50

6 Answers 6

2

You have an off-by-one access of your array: you are accessing the a[7] element but the last element of your array is a[6].

Look you have:

i++;
if( i == size  ) {

but above you are accessing a[i + 1] which means at some point you will access a[size] (which is outside the array).

Change if (i == size) to if (i == size - 1) to fix your issue.

Sign up to request clarification or add additional context in comments.

1 Comment

@Shinz6 I see you asked three questions and you didn't accept any answers. Thanks to accept answers for the question you asked if you feel they helped you: meta.stackexchange.com/questions/5234/…
0
  • You don't initialize min correctly, it needs to be set to the smallest possible number it can have, for example the constant INT_MIN found in limits.h. Instead of doing this, you overwrite min in each recursive call with the line min = a[ i ];.
  • You access the array out-of-bounds, in the last function call when i is 6, you run the code [ i + 1 ] which invokes undefined behavior. Unfortunately your program didn't crash, but instead outputs some garbage value.
  • There is absolutely no reason to use recursion for this algorithm you are writing.

1 Comment

Thanks a lot, I used recursion only because the exercise required so.
0

Show this code (I add integer variable static int initialize_min for initialization min as a[0] in first function call):

#include<stdio.h>
#define SIZE 7

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

int main(void) {
    int a[ SIZE ] = { 5, 7, 4, 3, 5, 1, 3 }; // Number 2 is not initialized.

    printf( "The smallest number is %d", recursiveMinimum( a, SIZE ) );

    return 0;
}

int recursiveMinimum( int a[], int size ) {
    static int min;
    static int initialize_min = 1;
    static int i = 0;


    if(initialize_min )
    {
        min = a[0];
        initialize_min = 0;
    }



    if( a[ i ] < min ) {
        min = a[ i ];
    }

    i++;

    if( i == size  ) {
        return min;
    } else {
       return recursiveMinimum( a, size  );
    }
}

Comments

0

I think this example is what is intended for a recursive minimum function:

#include <stdio.h>
#define SIZE 7

#define min(a, b)  (((a) < (b)) ? (a) : (b))

/*      assumes size != 0 */
int recursiveMinimum(int a[], size_t size){
int *beg;               /* ptr to beginning */
int *mid;               /* ptr to middle */
int *end;               /* ptr to end */
    if(size < 2)        /* if size == 1 */
        return a[0];
    beg = &a[0];        /* else split array */
    mid = &a[size/2];   /*  and recurse */
    end = &a[size];
    return min(recursiveMinimum(beg, mid-beg),
               recursiveMinimum(mid, end-mid));
}

int main(void)
{
    int a[SIZE] = {5, 7, 4, 3, 5, 1, 3 };
    printf( "The smallest number is %d", recursiveMinimum( a, SIZE ) );
    return 0;
}

Comments

0

You could try this method:

double smaller(double a, double b){
  return (a<b)?a:b;
}

double min(double *p_array, int idx_low, int idx_high){
  if(idx_low==idx_high)
     return p_array[idx_low];
  int idx_mid=idx_low+(idx_high-idx_low)/2;
  return smaller(min(p_array,idx_low,idx_mid), min(p_array,idx_mid+1, idx_high));
}

An analysis of the algorithm should give you an O(n*log(n)) running time - take it with a pinch of salt, though.

Comments

-1

To use INT_MIN, we should include limits.h. Also it must be unsafe, because we may want use unsigned, long, long long, __int8 etc.

1 Comment

Since this is C, the function parameters and return value would have to be changed to use a different type. If this was C++ and a template function, then dealing with a size == 0 case would be an issue.

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.