Thread: recursive finding index of the min number in array??

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    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!!???

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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;
    Last edited by itCbitC; 01-17-2009 at 12:48 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Your missing a return for your recursive steps. the last line should be return followerd by your function call.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    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!!

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How do you call it? Post the main function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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.
    Last edited by BEN10; 01-18-2009 at 07:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 09:37 AM
  2. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM