C Program To Find Biggest Element of An Array using Recursion

Write a C program to find biggest element / number in an array using pointers and recursion.

We have covered both these logic in this video tutorial
1. Recursive function with no return type.
2. Recursive function with return type.

Related Read:
C Program To Find Biggest Element of An Array
Recursive Functions In C Programming Language
Basics of Pointers In C Programming Language
Introduction To Arrays: C Programming Language

Important Video Tutorial
C Programming: Arrays, Pointers and Functions

Example: Expected Output

Enter 5 integer number
5
2
6
4
3
Biggest Element in the array: 6

Visual Representation

Biggest Element In An Array using Recursion

Video Tutorial: C Program To Find Biggest Element In An Array using Recursion


[youtube https://www.youtube.com/watch?v=-Cdp8QanJv8]

YouTube Link: https://www.youtube.com/watch?v=-Cdp8QanJv8 [Watch the Video In Full Screen.]

Source Code: Find Biggest Element of An Array using Recursion: With No Return Type

Method 1: With No Return Type

#include<stdio.h>

#define N 5

void biggest(int *num, int n, int big)
{
    if(n < 0)
        printf("Biggest element is %d\n", big);
    else
    {
        if(*num > big)
            big = *num;

        biggest(++num, --n, big);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    biggest(a, N - 1, a[0]);

    return 0;
}

Output:
Enter 5 integer number
1
2
3
4
5
Biggest Element in the array: 5

Logic To Find Biggest Element of An Array using Recursion

We ask the user to enter N integer numbers and store it inside array variable a[N]. We pass base address(address of first element in the array) of the array, which is present in &a[0] or a, and last index of the array(indicating size of the array, from index 0), and first element of the array(assuming first element itself as big).

Inside Recursive function
Base Condition: This is the condition to terminate the recursive call. Here we check if the size of the array is less than zero. If it’s less than zero, then we display the value present inside variable big, which holds the biggest element in the array.

void biggest(int *num, int n, int big)
{
    if(n < 0)
        printf("Biggest element is %d\n", big);
    else
    {
        if(*num > big)
            big = *num;

        biggest(++num, --n, big);
    }
}

We need to take a pointer variable to accept the base address. Next we’ll have base condition. If base condition isn’t met – we check if the value present in variable big is less than value present at *num. If it’s true, then we transfer the value of *num to big. Next we increment the address of num by 1 and decrement the value of n by 1 and pass them to the same function along with the value of big. This is called recursive function call.

Once value of n is less than 0, we display the value of variable big, which holds the biggest element of the array.

Important Note:

1. Array elements are always stored in contiguous memory location.
2. A pointer when incremented always points to an immediately next location of its own type.

Source Code: Find Biggest Element of An Array using Recursion: With Return Value

Method 2: With Return Type

#include<stdio.h>

#define N 5

int biggest(int *num, int n, int big)
{
    if(n < 0)
        return big;
    else
    {
        if(big < *num)
            big = *num;

        return biggest(++num, --n, big);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer number\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));

    return 0;
}

Output:
Enter 5 integer number
1
2
5
3
4
Biggest Element in the array: 5

After repeatedly incrementing value of num and decrementing the value n, we’ll reach a point where value of n will be less than 0. That’s when all the comparisons end, and variable big will have biggest element of the array. This result will be returned to the calling function, which in turn returns the result to the calling function and so on ..until the result is returned to the first function call, which was from with in main method – where we print the value of variable big.

Source Code: Using Array Variable In Recursive Function

Find Biggest Element of An Array using Recursion

#include<stdio.h>

#define N 5

int biggest(int num[], int n, int big)
{
    if(n < 0)
        return big;
    else
    {
        if(big < num[n])
            big = num[n];

        return biggest(num, --n, big);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer number\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));

    return 0;
}

Output:
Enter 5 integer number
10
56
83
978
4
Biggest Element in the array: 978

Here we are taking array variable to receive the base address. We keep checking if a[n] is biggest than value present at variable big. If true, then we transfer a[n] value to variable big, and then recursively call the same function by decrementing the value of n by 1, and also pass the new value of big.

Once the value of n is less than 0, we return the value present in variable big, which holds the biggest element of the array.

Explanation With Example

N = 5;
a[N] = {5, 2, 6, 4, 3};
n = N – 1 = 5 – 1 = 4;
num = a[0] = 5;
big = a[0] = 5;

biggest(num, --n, big);
nnum[n]big
435
345
266
126
056
-16

Biggest Element in the array: 6

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Function / Methods In C Programming Language

In today’s video tutorial lets learn the basics of function or methods in C programming language.

Function / Method: is a group of statements that together perform certain task.

Note: Every C program should have 1 main function. No 2 functions / methods should be named as main.

Video Tutorial: Function / Methods In C Programming Language


[youtube https://www.youtube.com/watch?v=X7auVd3QpTM]

YouTube Link: https://www.youtube.com/watch?v=X7auVd3QpTM [Watch the Video In Full Screen.]

Advantages of Function/Methods

1. It provides modularity to the program structure.
2. You can reuse your code. You can write function definition once and call the function any number of times in your program.
3. Easy to read, understand, edit and debug your code.

Types of Functions/Methods

1. Built in functions/methods.
2. User defined functions/methods.

1. Built in functions/methods: So far we’ve used a lot of builtin methods like pow(), sin(), cos(), tan(), sqrt() etc which are all present in library / header file math.h

If you open math.h header file and check the code, it’ll have function prototype and function definition for pow(), sin(), cos(), tan(), sqrt() etc.

Functions like printf(), scanf() and other input output functions/methods are present in stdio.h library file.

Similarly, if we want to write user defined function, we must specify the function prototype and function definition ourselves.

2. User defined functions/methods: We can define and use our own functions. We can have 4 types of user defined functions/methods.
a. No return type, no arguments.
b. No return type, with arguments.
c. With return type, no arguments.
d. With return type, with arguments.

Stay subscribed to our YouTube channel and blog for video tutorials explaining all types of user defined functions.

General Syntax of user defined Funtion/Method

1. Function prototype.
return_type funtion_name(argument_data_type_list);

2. Function definition.
return_type funtion_name(argument_data_type_list)
{
//instructions
}

3. Function Call.
funtion_name(argument_data_type_list);

Source Code: Function / Methods In C Programming Language: With Return Type and With Arguments

#include<stdio.h>

int add(int, int); // function prototype

int main()
{
    int a = 10, b = 20, c;

    c = add(a, b); // function call

    printf("Addition of %d and %d is %d\n", a, b, c);

    return 0;
}

//function definition
int add(int i, int j)
{
    return(i+j);
}

Output
Addition of 10 and 20 is 30

Source Code: Function / Methods In C Programming Language: No Return Type and No Arguments

#include<stdio.h>

void companies(); // function prototype

int main()
{
    companies(); // function call

    return 0;
}

//function definition
void companies()
{
    printf("1. IBM\n");
    printf("2. Apple\n");
    printf("3. Google\n");
    printf("4. Oracle\n");
    printf("5. Ripple\n");
}

Output
1. IBM
2. Apple
3. Google
4. Oracle
5. Ripple

Source Code: Function / Methods In C Programming Language: No Return Type and With Arguments

#include<stdio.h>

void multiply(int, int); // function prototype

int main()
{
    int a = 9, b = 12;

    multiply(a, b); // function call

    return 0;
}

//function definition
void multiply(int x, int y)
{
    printf("%d x %d = %d\n", x, y, (x*y));
}

Output
9 x 12 = 108

Source Code: Function / Methods In C Programming Language: With Return Type and No Arguments

#include<stdio.h>

int subtract(); // function prototype

int main()
{
    printf("Subtraction Result = %d\n", subtract() ); // function call

    return 0;
}

//function definition
int subtract()
{
    int a = 50, b = 25, c;

    c = a - b;

    return(c);
}

Output
Subtraction Result = 25

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert