Finding the largest number recursively requires us to call the function from with in the function. In the first call we pass the complete array, index of last element and largest element (initialised to the first element of the array). In search recursion call, we compare the current largest element to the current value at the index. If the value at current index is greater than the largest element, we set this as the new largest element. And call the function again with new index = index -1. This way with each recursion we reach the first element of the array and this is when we exit the function call. At the point in time we will have the number which is largest in the array.
C program to find the maximum element in an array using recursion.
#include<stdio.h> #include<stdlib.h> #include<time.h> #define MAX_SIZE 10 /* C program to find the largest element in a linear array of integers * recursively */ /* find_large takes the array we need to search in, index of the array to * compare with the largest number. Initially, the index will be that of the * last element of the array, and the first element will be set as the * largest element. With each recursion we update the largest element and * update the index so that we are moving towards the start of the array. * We stop once we reach the start of array and return the largest element * to the main function. */ int find_large(int arr[], int index, int largest) { if(index == 0) return largest; else { if(arr[index] > largest) largest = arr[index]; return find_large(arr,index-1,largest); }// end of if(index > 0) }// end of find_large int main() { int size, largest, i; // initialising the array with zero int arr[MAX_SIZE]={0,0,0,0,0,0,0,0,0,0}; // Take array size from user. size <= MAX_SIZE printf("Enter the array size (max size: %d): ",MAX_SIZE); scanf("%d", &size); // exit if size > MAX_SIZE if(size > MAX_SIZE) { printf("size entered is greater than MAX_SIZE, exiting!!!\n"); exit (1); } // exit if size is negative if(size < 0) { printf("How can I create negative sized array!!, exiting\n"); exit (1); } // exit if size == 0 if(size == 0) { printf("Empty array, exiting\n"); exit (1); } // setting current time as seed for random generator srand(time(0)); // Add random elements to the array printf("Adding elements to the array and printing them\n"); for(i = 0; i < size; i++) { arr[i] = rand() % 100; printf("%d, ",arr[i]); } printf("\n"); // initialising largest with first element of the array largest = arr[0]; /* calling find_large recursively with index as index of last element and * largest initialised with first element in the array */ largest = find_large(arr, size-1, largest); // printing the largest number after find_large returns printf("Largest number is %d\n",largest); return 0; }//end of main ############################ Output: Enter the array size (max size: 10): 11 size entered is greater tham MAX_SIZE, exiting!!! ############################ Enter the array size (max size: 10): -7 How can I create negative sized array!!, exiting ############################ Enter the array size (max size: 10): 0 Empty array, exiting ############################ Enter the array size (max size: 10): 8 Adding elements to the array and printing them 53, 11, 50, 18, 70, 69, 39, 97, Largest number is 97 ############################ Enter the array size (max size: 10): 3 Adding elements to the array and printing them 67, 9, 49, Largest number is 67
Hope you have enjoyed reading C program to find maximum number in an array by recursion. However, recursive solution in this case has no advantage over iterative one because there is more overhead associated with making recursive calls due to the fact that all recursive calls are saved at call stack.
Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Share this page on WhatsApp