# recursive finding index of the min number in array??

• 01-17-2009
kavka3
recursive finding index of the min number in array??
Code:

```int min_num(int arr[],int index, int minIndex, int size){         if (index==size)                 return minIndex;         else                 if (arr[index]<arr[minIndex])                         minIndex=index;                 min_num(arr,index+1,minIndex,size); }```
size=4;
index=1;
minindex=1;

if i have array={3,2,5,1}
i need find index of the minimum number in the array

minIndex=3, because 1 is the minimum number,,
but this returning 0!! why??
how i can return 3!!???
• 01-17-2009
itCbitC
array subscripts start at zero in C so go over this again
Code:

`if (index==size)`
Edit: never mind I did not read the code only the section below.

size=4;
index=1;
minindex=1;
• 01-17-2009
King Mir
Your missing a return for your recursive steps. the last line should be return followerd by your function call.
• 01-18-2009
kavka3
Code:

```int min_num(int arr[],int index, int minIndex, int size){         if (index==size)                 return minIndex;         else                 if (arr[index]<arr[minIndex])                         minIndex=index;                 return min_num(arr,index+1,minIndex,size); }```
it doesn`t help
still returning 0!!
• 01-18-2009
anon
How do you call it? Post the main function.
• 01-18-2009
BEN10
since array indexing starts at 0,thus u should pass the value of index as 0.if index=1 is passed the code will start comparing the elements from arr[1] leaving the first element ignored.also if u're calling this function from main it should return its value to some variable and that variable should get printed.
in main
Code:

`k=min_num(arr,0,1,4);`
hope it helps.