Size of an array using Recursion in C

by

Posted on 28 January 2017

Most of us have done a lot of C programs using recursion and Recursion is the only thing in programming which gives me nightmarescry but anyways today we will see another C program which we can make using recursion.

In this tutorial we will learn how to create a C program to count the size of array using Recursion So let's get started

Size of an array using recursion in C

Below C code is to find the size of an array using recursion

#include<stdio.h>
int count(int arr[], int size);
int main()
{
    int i, arr[100], n, len;
    printf("Size of array?\n");
    scanf("%d",&n);

for(i=0; i<100; i++)
{
    arr[i]='\0';     //loop to reset the array
}

    printf("Enter terms except 0:\n"); 
    for(i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }
    len=count(arr, 0); //store the length
    printf("Size is %d\n",len);
return 0;
}

int count(int arr[], int size)
{
    if(arr[size]!='\0')
        return 1+count(arr,++size);
    else
        return 0;
}

Output

size of an array using recursion in C

Explanation:

Variable ‘arr[100]’ stores the terms in an indexed array with size = 100.

Variable ‘n’ holds the size of the array entered by the user

Variable ‘len’ holds the length of the array calculated by recursion

Variable ‘i’ is used as counter in the loops

for(i=0; i<100; i++)

{

    arr[i]='\0';     //loop to reset the array

}

The above loop resets the arr[] declared to remove any garbage value stored previously, because program may behave abnormally if it encounters ‘0’.

for(i=0; i<n; i++)

    {

        scanf("%d",&arr[i]);

    }

The above loop stores the value of the array from the user

len=count(arr, 0);

The above statement calls the function “int count(int arr[], int size)” sending array ‘arr’ and size ‘0’

if(arr[size]!='\0')

        return 1+count(arr,++size);

else

        return 0;

The above statement is the logic we use for calculating the size of array, as we already know that when an array terminated ‘\0’ is used for indicating the end of array, So the above statement recursively calls the function ‘count()’ while adding ‘1’ to the value returned by the recursively called ‘count()’  until we find ‘\0’ indicating the end of array.

If found, we ‘return 0’


What happens when ‘\0’ is found?

The last function called returns the value ‘0’, which gets sent to the function previoiusly stored on the stack memory waiting for a return value, so ‘return 1+count(arr,++size)’ gets ‘1+0’ then ‘1+1’ then 1+2’, depending upon how many function calls were stored.


Ex: Consider len=count(arr, 0);

    Where arr[]={1,2,3};

    The count function will work as below

C program to find size of array using recursion

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts

Search
Adverstisement
Adverstisement