I've written a little program to determine the minimum element in an array a[]
. When I debug, the program seems to be working fine initially but then the for loop in the min()
function stops execution after two steps, despite the array being of size 10.
The final output is 23, when it sould be 3.
Is the code sizeof(a)/sizeof(int)
incorrect? I found it on another article on stackoverflow.
#include <stdio.h>
#include <limits.h>
int min(int[]);
int main(){
int a[]={100,23,3,4,5,6,7,8,9,10};
printf("%d", min(a));
return 0;
}
int min(int a[]){
int min = INT_MAX, i;
for(i = 0; i < sizeof(a)/sizeof(a[0]); i++){
if((i>=0) && (a[i]< min))
min = a[i];
}
return min;
}