Introduction
This lab will walk you through the process of finding the largest element in an array using recursion. The C program will prompt the user to enter the size of the array, then input the elements of the array, followed by outputting the largest element of the array.
Setting up the File
First, create a new file named main.c in the ~/project/ directory. Then, copy the code below and paste it into your file, which contains all the code we need to complete this lab.
#include<stdio.h>
#define MAX 100
int getMaxElement(int []); // takes array of int as parameter
int size;
int main()
{
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
int arr[MAX], max, i;
printf("\n\nEnter the size of the array: ");
scanf("%d", &size);
printf("\n\nEnter %d elements\n\n", size);
for(i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
max = getMaxElement(arr); // passing the complete array as parameter
printf("\n\nLargest element of the array is %d\n\n", max);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
int getMaxElement(int a[])
{
static int i = 0, max =- 9999; // static int max=a[0] is invalid
if(i < size) // till the last element
{
if(max < a[i])
max = a[i];
i++; // to check the next element in the next iteration
getMaxElement(a); // recursive call
}
return max;
}
Understanding the Code
This C program consists of two functions: main() and getMaxElement().
2.1 Function main()
- Declares an array named
arrto store the elements of the array and initializes variables:maxto store the largest element of the array, andito maintain the iteration of the function. - Asks the user to enter the size of the array.
- Asks the user to input the elements of the array.
- Calls
getMaxElement()function and passes the array namedarras its parameter. - Outputs the largest element of the array stored in the variable
max.
2.2 Function getMaxElement()
This is a recursive function that returns the largest element of the array
- Declares a static value
ito maintain the iteration of the function and initializes it to 0, and a static valuemaxto store the largest element of the array and initializes it to a very small value-9999. - Checks if the value of
iis less than the size of the array. - If the value of the
ith element of the array is greater than the current value ofmax, then the value ofmaxis updated with theith element of the array. - Sets
ito the next element and callsgetMaxElement()function, making this function recursive.
Testing the Program
To compile and run the code, open a terminal in the ~/project/ directory and follow the steps below:
- Type
gcc main.cto compile the code. - Type
./a.outto run the program. - In the prompt, enter the size of the array and press
ENTER. - Enter the elements of the array and press
ENTERafter each one. - The program will output the largest element of the array.
Full Code
#include<stdio.h>
#define MAX 100
int getMaxElement(int []); // takes array of int as parameter
int size;
int main()
{
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
int arr[MAX], max, i;
printf("\n\nEnter the size of the array: ");
scanf("%d", &size);
printf("\n\nEnter %d elements\n\n", size);
for(i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
max = getMaxElement(arr); // passing the complete array as parameter
printf("\n\nLargest element of the array is %d\n\n", max);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
int getMaxElement(int a[])
{
static int i = 0, max =- 9999; // static int max=a[0] is invalid
if(i < size) // till the last element
{
if(max < a[i])
max = a[i];
i++; // to check the next element in the next iteration
getMaxElement(a); // recursive call
}
return max;
}
Summary
Great job! You have successfully completed this lab on how to find the largest element in an array using recursion. You should now have a good understanding of how to use recursion to find the largest element of an array. Well done!



