finding max in array using RECURSION

This is a discussion on finding max in array using RECURSION within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int getMax(int [], int, int, int); int main(void) { int arr[10]={0}, i; printf("Enter the 10 elements: "); ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    Unhappy finding max in array using RECURSION

    Code:
    #include <stdio.h>
    
    
    int getMax(int [], int, int, int);
    int main(void)
    {
    	int arr[10]={0}, i;
    	printf("Enter the 10 elements: ");
    	for(i=0; i<10; i++)
    	{
    		scanf("%d", &arr[i]);
    	}
    	
    	printf(" Maximum number is %d \n", getMax(arr, 10, 0, 0));
    	
    	return 0;
    }
    
    
    int getMax(int arr[], int size, int max, int i)
    {
    	if(i<size)
    	{
    		if(max<arr[i])
    			max= arr[i];
    
    
    		getMax(arr, size, max , i++);
    	}
    	
    	return max;
    }
    Hi, this is my code to find max number in the array using recursion. It cant run. cmd keep stopping! i tried to debug alot of time but just dont get it! Please help! Thanks!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Don't you want to use the return value? And you shouldn't be incrementing i, you just want to pass the next index to the function.
    Code:
    int getMax(int arr[], int size, int max, int i)
    {
        if(i<size)
        {
            int rv;
    
            if(max<arr[i])
                max= arr[i];
     
     
            rv = getMax(arr, size, max , i + 1);
            if(max<rv)
                max = rv;
        }
         
        return max;
    }
    Last edited by itsme86; 10-27-2011 at 09:35 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    18
    Quote Originally Posted by itsme86 View Post
    Don't you want to use the return value?
    Code:
    int getMax(int arr[], int size, int max, int i)
    {
        if(i<size)
        {
            int rv;
    
            if(max<arr[i])
                max= arr[i];
     
     
            rv = getMax(arr, size, max , i++);
            if(max<rv)
            max = rv;
        }
         
        return max;
    }
    i tried but it doesnt work still.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I added more changes, look at my previous post again.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    18
    Quote Originally Posted by itsme86 View Post
    I added more changes, look at my previous post again.
    Code:
    int getMax(int arr[], int size, int max, int i){
    	if(i<size)
    	{
    		if(max<arr[i])
    			max= arr[i];
    		getMax(arr, size, max , i+1);
    	}
    	
    	return max;
    }
    This works as well! Thanks! I just change i++ to i+1. I thought they both mean the same thing, why i++ will cause error?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Let's assume i is 10. i++ evaluates to 10, but assigns 11 to i. i + 1, on the other hand, evaluates to 11 and leaves i at 10.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A recursion array scanner
    By steelsoul in forum C Programming
    Replies: 4
    Last Post: 12-12-2010, 05:12 PM
  2. Replies: 8
    Last Post: 06-14-2009, 01:39 PM
  3. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  4. creating combinations of array values - recursion??
    By johndirect in forum C Programming
    Replies: 2
    Last Post: 11-20-2008, 11:49 AM
  5. finding an int in an array
    By Geo-Fry in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2003, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21