2

I have coded the following program to find the minimum value element from an array using recursion. However the program keeps on showing me the answer as 1000

#include<stdio.h>
#define MAX 100

int getminElement(int []);
int size;

int main(){

    int min;
    int i;
    int a[10]={12,6,-24,78,43,3,22,45,40};

    min=getminElement(a);

    printf("Smallest element of an array is: %d",min);

    return 0;
}

int getminElement(int a[]){

    static int i=0,min =1000;

    if(i < size){
        if(min > a[i])
            min=a[i];
        i++;
        getminElement(a);
    }

    return min;
}
2
  • 2
    Decide first, you want minimum or maximum? Commented Dec 21, 2015 at 15:58
  • Maybe also decide what to do for empty arrays (i.e. size == 0). If you consider that case undefined (an assert might be good here) you could define that the minimum of a singleton array (i.e. size == 1) is the only element. Otherwise, it's the smaller value of the first array element and the minimum of the remainder of the array. Commented Dec 21, 2015 at 16:37

2 Answers 2

6

You did not set the size variable. Since size is a global variable, it is automatically getting initialized to 0.

So when this condition is reached for the first time

if(i < size)

It fails, thus returning min, which is 1000


Do the following

int max;
int i;
int a[10]={12,6,-24,78,43,3,22,45,40};

size = 10;

Or, to make it better.

int max;
int i;
int a[10]={12,6,-24,78,43,3,22,45,40};

size = sizeof(a)/sizeof(int);

As discussed in the comments, there is another error in your code. You ignore the return value of the function call inside the if block.

You should use that to return the value back to the calling function. Something like,

if(i < size)
{
    if(min > a[i])
        min=a[i];
    i++;
    return getminElement(a);
}

As pointed out by some people, your code is a little confusing, so I'll post a simpler one, without static and global variables. Do try to understand it, and post comment if you find it difficult.

#include<stdio.h>
#define MAX 100

int getminElement(int [], int, int);

int main()
{

    int min;
    int i;
    int a[10]={12,6,-24,78,43,3,22,45,-40};

    printf("Smallest element of an array is: %d", getminElement(a, 0, sizeof(a)/sizeof(int)));

    return 0;
}

int getminElement(int a[], int index, int size)
{
    int min = 9999;

    if(index < size)
        min = getminElement(a, index + 1, size);

    if(a[index] < min)
        return a[index];
    else
        return min;

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

14 Comments

@haccks, are you talking about the max-min thing?
I am talking about getminElement function.
@Haris; Look at the statement getminElement(a); in getminElement function. Its return type is int.
In getminElement(a); the value returned by that recursion is ignored.
@CoolGuy, Yes, but it should not be ignored, I agree with haccks.
|
1

Another recursive version

int getminElement(int min, int *p, int size) {
   if (*p < min) min = *p;    // set min to minimum(*p, min)
   if (size < 1) return min;  // no more elements to see, return min
   return getminElement(min, p+1, size-1); // recursive call
}

called

int numelem = sizeof(a) / sizeof(*a);
int minimum = getminElement(1000, a, numelem);

assuming the minimum in a being >= 1000, as you did.

Here the getminElement function examine all elements of a, via the pointer p to int which is incremented at each recursive call, size being decremented.

The function being called recursively with the current min, the next element to examine, the remaining size.

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.