Q:

C Program to find the Biggest Number in an Array of Numbers using Recursion

0

C Program to find the Biggest Number in an Array of Numbers using Recursion

 Write a C Program to find the Biggest Number in an Array of integers (can be negative too) using Recursion.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Algorithm:

1. Construct a max function which will return maximum of two.
    Function max(a, b)
        return a>b? a: b; //using ternary operator
    End Function

2. Construct recursive functionfindBigRec (array, end index)
    Function findBigRec (array, end index)
        a.  Base case:
                IF end index==0
                    return INT_MIN;

        b.  Return maximum between the last element (array [end index-1]) & 
            maximum of rest of the array. Maximum of rest of the array 
            is calculated recursively.
            return max (array[end index-1], findBigRec (array, end index-1));

Explanation with example:

Let the length of input array: 6
Array elements: 4 12 5 13 7 9
In the main function:

Call findBigRec (array,6);
---------------------------------------------------
findBigRec (array,6):
end index, 6 != 0
return max( array[5], findBigRec (array,5));

Call findBigRec (array,5);
---------------------------------------------------
findBigRec (array,5):
end index, 5 != 0
return max( array[4], findBigRec (array,4));

Call findBigRec (array,4);
---------------------------------------------------
findBigRec (array,4):
end index, 4 != 0
return max( array[3], findBigRec (array,3));

Call findBigRec (array,3);
---------------------------------------------------
findBigRec (array,3):
end index, 3 != 0
return max( array[2], findBigRec (array,2));

Call findBigRec (array,2);
---------------------------------------------------
findBigRec (array,2):
end index, 2 != 0
return max( array[1], findBigRec (array,1));

Call findBigRec (array,1);
---------------------------------------------------
findBigRec (array,1):
end index, 1 != 0
return max( array[0], findBigRec (array,0));

Call findBigRec (array,0);
---------------------------------------------------
findBigRec (array,0):
end index, 0 == 0
return INT_MIN;
---------------------------------------------------

findBigRec (array,1) returns max ( array[0], findBigRec (array,0))
				=max (4, INT_MIN)
				=4
---------------------------------------------------
findBigRec (array,2) returns max ( array[1], findBigRec (array,1))
				=max (12, 4)
				=12
---------------------------------------------------
findBigRec (array,3) returns max ( array[2], findBigRec (array,2))
				=max (5, 12)
				=12
---------------------------------------------------
findBigRec (array,4) returns max ( array[3], findBigRec (array,3))
				=max (13, 12)
				=13
---------------------------------------------------
findBigRec (array,5) returns max ( array[4], findBigRec (array,4))
				=max (7, 13)
				=13
---------------------------------------------------
findBigRec (array,6) returns max ( array[5], findBigRec (array,5))
				=max (9, 13)
				=13
Thus max returned by main function is 13
Biggest no in the array is 13

C implementation to find the Biggest Number in an Array of Numbers using Recursion

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

//finding maximum of two element
int max(int a,int b){
	return (a>b)?a:b;
}

int findBigRec(int* a,int n){
	//base case
	if(n==0)
		//can't return 0, since there may be 
		//negative numbers in the array
		return INT_MIN; 
	//recursively process
	//return maximum between the last element 
	//& maximum of rest of array
	//maximum of rest of array is again going 
	//to be recursively processed
	return max(a[n-1],findBigRec(a,n-1));
}

int main()
{   
	int n;
	
	printf("Enter array length: ");
	scanf("%d",&n);
	
	int* a=(int*)(malloc(sizeof(int)*n));
	printf("enter elements...\n");
	//input array elements
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	
	//recursive function to find the maximum of the array
	int big=findBigRec(a,n);

	printf("The biggest element in the array is: %d\n",big);

	return 0;
}

Output

Enter array length: 6
enter elements...
4 12 5 13 7 9
The biggest element in the array is: 13 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to print the number of subset whose elem... >>
<< C program to accept Sorted Array and do Search usi...