Reverse Array using Recursion in C Programming
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:
1 2 |
Enter desired array size(1-100): 10 Please enter array elements(10) : 100 200 300 400 500 600 700 800 900 999 |
Output:
1 2 |
Original Array: 100 200 300 400 500 600 700 800 900 999 Reversed Array: 999 900 800 700 600 500 400 300 200 100 |
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.
- C Array – Create and Use Arrays in C
- Functions in C – Declare, Define, and Call functions
- Recursion in C Language with Stack flow
- Passing Arrays to the functions in C programming
Reverse Array using Recursion in C Program Explanation:
- Declare the numbers[100] array, which holds the 100 elements at max.(update the size if needed)
- Take the desired array size from the user and update the size variable.
- Define three functions called read function, display function, and reverseArr functions. Let’s discuss about these functions in detail
- The
read() function is used to read the user input and update the
numbers array.
- Prototype of the read function – void read(int numbers[], int size)
- The read() function takes two formal arguments. which are numbers[] array and its size.
- Read function takes user input and updates the array elements.
- Note that, The numbers array is passed with the address, so the changes made in the read() function will reflect on the calling function ( main()).
- 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)
- 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
- Now, Call the read() function from the main function to take the user input.
- Then print the original array by calling the display() function with respective parameters.
- 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.
- Print the reversed array by calling the display() function again.
Algorithm to Reverse Array using Recursion in C:
- Start the program
- Declare numbers[100] array, and a few supporting variables like i, size, start, end;
- Take the desired sizeof the array.
- 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
- 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
- Update the start=0 and end=size-1
- 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.
- Print the reversed Array by calling the display() function again.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/* Program to Reverse an array using recursion sillycodes.com */ #include <stdio.h> /** * @brief - Read the user input and update the 'numbers' array * * @param numbers - 'numbers' array * @param size - size of the 'numbers' array */ void read(int numbers[], int size) { int i; // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("numbers[%d] : ", i); scanf("%d", &numbers[i]); } } /** * @brief - Display the 'numbers' array * * @param numbers - Array * @param size - Array size */ void display(int numbers[], int size) { int i; for(i = 0; i < size; i++) { printf("%d ", numbers[i]); } printf("\n"); } /** * @brief - 'reverseArr' reverses array by swapping elements recursively * * @param numbers - 'numbers' array * @param start - 'start' index * @param end - 'end' index */ void reverseArr(int numbers[], int start, int end) { int temp; if (start >= end) { // base condition return; } // swap the 'start' and 'end' index elements temp = numbers[start]; numbers[start] = numbers[end]; numbers[end] = temp; // make a recursive call to 'reverseArr' with updated 'start' and 'end' return reverseArr(numbers, start+1, end-1); } int main() { // declare the 'numbers' array int numbers[100]; int i, size, start, end; printf("Enter desired array size(1-100): "); scanf("%d", &size); // Take user input and update array read(numbers, size); // print the original array printf("Original Array: "); display(numbers, size); start = 0; end = size-1; // Call the 'reverseArr' and pass 'start' and 'end' reverseArr(numbers, start, end); // display the reversed array printf("Reversed Array: "); display(numbers, size); return 0; } |
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:
1 2 3 4 5 6 |
$ ./a.out Enter desired array size(1-100): 6 Please enter array elements(6) : 65 55 45 35 25 15 Original Array: 65 55 45 35 25 15 Reversed Array: 15 25 35 45 55 65 $ |
Test Case 2:
1 2 3 4 5 6 7 8 9 10 11 |
$ ./a.out Enter desired array size(1-100): 10 Please enter array elements(10) : 100 200 300 400 500 600 700 800 900 999 Original Array: 100 200 300 400 500 600 700 800 900 999 Reversed Array: 999 900 800 700 600 500 400 300 200 100 $ ./a.out Enter desired array size(1-100): 4 Please enter array elements(4) : -1 -5 -8 -9 Original Array: -1 -5 -8 -9 Reversed Array: -9 -8 -5 -1 $ |
As we can see from the above output, The program is generating the expected results.
Related Array Programs:
- Index – C Language Practice Programs
- C Tutorials Index – Step by step tutorials to master C Langauge
- C Program to Read and Print Arrays
- Reverse Print Array Program in C
- C Program to calculate Sum of all array Elements
- C Program to Calculate the Average of all array Elements
- C Program to find minimum element in Array
- C Program to calculate the total number of Positive numbers, Negative numbers, and Zeros in an Array
- C Program to Count Even and Odd numbers in Array
- C Program to Find Maximum Element in Array
- C Program to Reverse the Array Elements
2 Responses
[…] C Program to Reverse Array Elements using Recursion […]
[…] C Program to Reverse Array Elements using Recursion […]