Reverse an Array in c/c++

Write a Program to reverse an array in c/c++.

Example,

Input: 5 4 1 2 4 6
Output: 6 4 2 1 4 5

It can be implemented in 2 way:-

  1. Without Recursion (Iteratively)
  2. using Recursion (Recursively)

Reverse Array without Recursion (Iterative)

In this method, we simply swap the first element with last element, the second element with the second last element, and so on.

See the following illustration

reverse array without recursion iteratively

Algorithm

Let arr be the array. Let N be the size of arr.

  1. Initialize i = 0, j = N-1.
  2. Repeat following steps while i<j
    • temp = arr[i].
    • arr[i] = arr[j].
    • arr[j] = temp.
    • ++i, –j.

Pseudo Code

arr: Array we want to reverse
N: Size of array
FUNCTION ReverseArr ( arr , N )
       i = 0
       j = N – 1
       WHILE ( i < j ) DO
              temp = arr[i]
              arr[i] = arr[j]
              arr[j] = temp
              ++i
              --j
       END WHILE
END FUNCTION

Program to Reverse Array in C/C++ (Iteratively)

#include<stdio.h>

// function to reverse array
int ReverseArr(int *arr, int n){
	int i,j,temp;
	i = 0;
	j = n -1;
	while(i<j){
		// swapping arr[i] and arr[j]
		temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
		++i;
		--j; 
	}
}

int main()
{		
	int arr[100];
	int n,i;
	
	printf("\nEnter Size of Array: ");
	scanf("%d",&n);
	
	printf("\nEnter Array Elements: ");
	for(i = 0;i<n;++i){
		scanf("%d",&arr[i]);
	}
	
	ReverseArr(arr,n);
	
	printf("\nArray After Reverse: ");
	for(i = 0;i<n;++i){
		printf("%d ",arr[i]);
	}
	printf("\n");
	
}

Output

output

Reverse Array using Recursion (Recursively)

This Method is very similar to iterative method. But instead of swapping array elements using loop, we use recursive calls.

First and Last elements are swapped in the first recursive call.

Second and Second Last elements are reversed in next recursive call and so on.

Algorithm

Let ReverseArr() be the recursive function.

It accepts 3 parameters: array, ith index from front, ith index from last.

ReverseArray(arr,i,j):

  1. If i >= j RETURN.
  2. Swap arr[i] and arr[j].
  3. ReverseArr(arr, i+1 , j-1)

Pseudo Code

arr: Array we want to Reverse
N: Size of Array

FUNCTION ReverseArr( arr, i ,j )

        IF i >= j THEN
                RETURN
        END IF

        temp = arr[i]
        arr[i] = arr[j]
        arr[j] = temp
        ReverseArr( arr , i+1, j-1 )

END FUNCTION

Program to Reverse Array using Recursion in c/c++ (Recursively)

#include<stdio.h>

// function to reverse array
void ReverseArray(int *arr, int i, int j){
	int temp;
	
	if(i>=j)
		return;
	
	temp = arr[i];
	arr[i] = arr[j];
	arr[j] = temp;
	
	ReverseArray(arr, i+1, j-1);
	
}

int main()
{		
	int arr[100];
	int n,i;
	
	printf("\nEnter Size of Array: ");
	scanf("%d",&n);
	
	printf("\nEnter Array Elements: ");
	for(i = 0;i<n;++i){
		scanf("%d",&arr[i]);
	}
	
	ReverseArray(arr, 0, n-1);
	
	printf("\nArray After Reverse: ");
	for(i = 0;i<n;++i){
		printf("%d ",arr[i]);
	}
	printf("\n");
	
}

Output

reverse array using recursion (recursively)

References

https://en.wikipedia.org/wiki/Array_data_structure

What to study next?

Reverse a Linked List
Reverse Linked List in Group

Leave Comment if you have any problem

Leave a Comment

Your email address will not be published. Required fields are marked *