Largest Element in an Array Using Recursion

CCBeginner
Practice Now

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\tStudytonight - 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 arr to store the elements of the array and initializes variables: max to store the largest element of the array, and i to 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 named arr as 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 i to maintain the iteration of the function and initializes it to 0, and a static value max to store the largest element of the array and initializes it to a very small value -9999.
  • Checks if the value of i is less than the size of the array.
  • If the value of the ith element of the array is greater than the current value of max, then the value of max is updated with the ith element of the array.
  • Sets i to the next element and calls getMaxElement() 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.c to compile the code.
  • Type ./a.out to run the program.
  • In the prompt, enter the size of the array and press ENTER.
  • Enter the elements of the array and press ENTER after 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\tStudytonight - 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!

Other C Tutorials you may like