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;
}
size== 0). If you consider that case undefined (anassertmight 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.