Reverse Array using Recursion in C Programming

reverse-array-using-recursion-in-c-language

Program Description:

Write a Program to Reverse Array using Recursion in C programming language. In an earlier tutorial, We have looked at the Reversing array elements using the Iterative method. In today’s article, we will use Recursion to reverse the array elements.

Here is the example input and output of the program

Expected Input and output:

Input:

Output:

Prerequisites:

It is recommended to know the basics of the Arrays, Functions and Recursion in C Langauge. Please go through the following articles to learn more about the above topics.

Reverse Array using Recursion in C Program Explanation:

  1. Declare the numbers[100] array, which holds the 100 elements at max.(update the size if needed)
  2. Take the desired array size from the user and update the size variable.
  3. Define three functions called read function, display function, and reverseArr functions. Let’s discuss about these functions in detail
  4. The read() function is used to read the user input and update the numbers array.
  5. The display() function is used to display all array elements on the console.
    • Prototype of the display function – void display(int numbers[], int size)
    • The display() function also takes the two formal arguments similar to the read function. The arguments are also the same.
    • The display() function simply prints the array elements on the console but doesn’t change anything in the numbers array.
    • This function doesn’t return any value back to the caller (the Return type is void)
  6. The reverseArr function is used to reverse the elements of the array recursively.
    • Protoype of the reverseArr is void reverseArr(int numbers[], int start, int end)
    • The reverseArr takes three formal arguments. which are numbers[] array and start and end variables.
    • The base condition for the recursion is the start >= end, So the recursion will continue until the variable start becomes equal or greater than the end variable. If the start>=end becomes true, Stop the recursion.
    • If the above condition is not true, Then swap the numbers[start] and numbers[end] array elements.
    • Then make the recursive call to reverseArr with start+1 and end-1
  7. Now, Call the read() function from the main function to take the user input.
  8. Then print the original array by calling the display() function with respective parameters.
  9. Now, We need to reverse array elements, So a function call to the reverseArr() function with numbers array and start, and end parameters. The reverseArr function reverses the numbers array elements using recursion.
  10. Print the reversed array by calling the display() function again.

Algorithm to Reverse Array using Recursion in C:

  1. Start the program
  2. Declare numbers[100] array, and a few supporting variables like i, size, start, end;
  3. Take the desired sizeof the array.
  4. Call the read() function and prompt the user to provide the array elements and update the array.
    • In the read() function, Loop from zero(0) to size-1 and update the numbers[i] element
  5. Call the display() function to print the array on Standard output.
    • In the display() function, Loop from zero(0) to size-1 and print the numbers[i] element
  6. Update the start=0 and end=size-1
  7. Make a function call to reverseArr() function pass the numbers, start, and end variables.
    • Use the (start >= end) as the base condition
    • If the base condition is false, Swap the elements using temporary variable.
    • Make a recursive call to reverseArr – reverseArr(numbers, start+1, end-1);
    • Continue the recursion until the base condition satisfied.
  8. Print the reversed Array by calling the display() function again.
  9. Stop the program.

Program to Reverse Array using Recursion in C Language:

Here is the program to reverse an array using recursion in c programming language.

Program Output:

Compile and Run the program using your compiler. we are using the GCC compiler in Ubuntu Linux

Compile the program

gcc reverse-array-recursive.c

Run the program.

Test Case 1:

reverse-array-in-c-using-recursion-program-output

Test Case 2:

As we can see from the above output, The program is generating the expected results.

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Reverse Array Elements using Recursion […]

  2. […] C Program to Reverse Array Elements using Recursion […]

Leave a Reply