this code gives the correct result upto size 6 (i'v run this a few times, and 6 seems to be the boundary), although the intermediate steps are all insane! then onwards, from size 7, it returns minimum value 0
/*minimum of an array using recursion*/
#include<stdio.h>
#include<stdlib.h>
int findMin(int* arr,int min,int size);
int* populateArray(int size);
void printArray(int* arr,int size);
int main(){
int i=0,size=0,min=0;
int* arr;
printf("enter size of array: ");
scanf("%d",&size);
arr=populateArray(size);
printf("array is: ");
printArray(arr,size);
min=*(arr+0);
min=findMin(arr,min,size);
printf("\n\nthe min value in the arr is: %d\n",min);
return 0;
}
int* populateArray(int size){
int i=0;
int* arr;
arr=(int*)malloc(sizeof(int)*size);
srand(time(NULL));
for(i=0;i<size;i++){
*(arr+i)=(rand()%1000);
}
return arr;
}
void printArray(int* arr,int size){
int i;
for(i=0;i<size;i++)
printf("%d ",*(arr+i));
puts("\n");
}
int findMin(int* arr,int min,int size){
printf("in findMin() : passed values >> array:%d min:%d size:%d\n",*(arr),min,size);
static int j=1;
if(j==size){
return min;
}
else if(min>*(arr+j)){
min=*(arr+j);
j++;
findMin((arr+j),min,size);
}
else if(min<*(arr+j)){
j++;
findMin((arr+j),min,size);
}
}
i cant understand why this is so... :(
any help regarding the matter would be great. :)
regards
somjit.