This is a C Program which prints the largest number in an unsorted array of elements using recursion.
This program will implement a one-dimentional array defining elements in unsorted fashion, in which we need to find largest element using Recursion method. The array used here is of type integer.
1. Create an array, taking its size from the users and define all its elements.
2. Now make a function passing three parameters, array, last index of array and largest element of the array.
3. Assuming first element to be the largest element in the array, call this function.
4. Inside this function, since first element has been selected as largest for now, check if the element at index passed to this function (initially last index) is greater than the largest number passed. Update the largest number and proceed to call the same function, but now with index-1.
5. This way each element is checked from index (size-1) to 1 is compared with largest number. The base condition for this recursive function would be to return the largest number when it reaches the first index.
Here is the source code of the C program to print the largest number in an unsorted array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to find the Biggest Number in an Array of Numbers using
* Recursion
*/
#include <stdio.h>
int large(int[], int, int);
int main()
{
int size;
int largest;
int list[20];
int i;
printf("Enter size of the list:");
scanf("%d", &size);
printf("Printing the list:\n");
for (i = 0; i < size ; i++)
{
list[i] = rand() % size;
printf("%d \t", list[i]);
}
if (size == 0)
{
printf("Empty list\n");
}
else
{
largest = list[0];
largest = large(list, size - 1, largest);
printf("\nThe largest number in the list is: %d\n", largest);
}
}
int large(int list[], int position, int largest)
{
if (position == 0)
return largest;
if (position > 0)
{
if (list[position] > largest)
{
largest = list[position];
}
return large(list, position - 1, largest);
}
}
1. Declare an array of some fixed capacity, 20 and take size from the users and define elements in unsorted fashion using rand() functions to insert random number less than size ( rand() % size )
2. Assume first element of the array to be the largest number.
2. Create a function with three parameters i.e the array, last index of array (size -1) and largest element of the array.
4. Call this function.
5. Inside it, the largest number is compared with the element at position passed to the function (initially the position passed is the last index of the array) and make necessary updation if largest number is smaller than the number compared.
6. After comparison, this function is again called inside itself but with position being previous position(last index)-1.
7. This way all elements are compared with largest number making updation wherever needed via recursion.
8. This process stops when the variable position reaches the 0th position.
Enter size of the list:8 Printing the list: 7 6 1 3 1 7 2 4 The largest number in the list is: 7
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check C Books
- Apply for C Internship
- Apply for Computer Science Internship