C Program to reverse the elements of an array

In this blog post, we learn how to write a C Program to reverse the elements of an array? So here will write the C Program to reverse the elements of an array. We will also see how to display array elements in reverse order.

So let’s see the logic to reverse the array elements. Suppose arr is an integer array of size N (arr[N] ), the task is to write the C Program to reverse the elements of the given array.

Examples:

//Input array
Input  : arr[] = {1, 2, 3}


//output array
Output : arr[] = {3, 2, 1}

 

Logic to reverse the elements of an array:

 

1. Create two intermediate variables start and end.

2. Initialize the start and end variable with 0 and n-1 (where n  is the number of elements in the array).

3. Now In the loop, swap arr[start] with arr[end] and update the value of start and end variables as follows :

//Update start
start = start +1;

//update end 
end = end – 1;

 

reverse array elements

C Program to reverse the elements of an array:

#include<stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])

int main()
{
    int arr[] = {1,2,3,4,5,6};
    // length of the array
    int N = ARRAY_SIZE(arr);
    // assign the 0 valid index
    int start = 0;
    // assign the last valid index
    int end = (N - 1);
    int tmp, i;


    while(start < end)
    {
        // swap the elements
        tmp = arr[start];
        arr[start] = arr[end];
        arr[end] = tmp;

        start++;
        end--;
    }

    // print the reversed array
    for( i = 0; i < N; i++)
    {
        printf("%d ", arr[i]);
    }

    return 0;
}

Output:

itterative method to reverse array elements

How does it work?

The above-mentioned C program is an iterative method to reverse array elements. Here in the loop, we are swapping the first element of the array with the last element, the second element with the second last element, and so on. We keep repeating this procedure until we reach halfway through the array.

Recursive method to reverse the elements of an array:

We can also use the recursive method to reverse the elements of an array. If you don’t know about the recursive method, you can read this article “How to use the recursive function in C?”.

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])

//Function to reverse arr[] from start to end
void rvereseArray(int arr[], int start, int end)
{
    int temp;
    if (start >= end)
    {
        return;
    }
    //swap array element
    temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    rvereseArray(arr, start+1, end-1);
}

//print the array
void printArray(int arr[], int size)
{
    int i =0;
    for (i=0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}


int main()
{
    // Given array
    int arr[] = {1, 2, 3, 4, 5, 6};

    // length of the array
    int N = ARRAY_SIZE(arr);

    int startIndex =0;
    int endIndex = N-1;

    //print array elements
    printArray(arr, N);

    //reverse the array elements
    rvereseArray(arr, startIndex, endIndex);

    printf("Reversed array is \n");

    //print array elements
    printArray(arr, N);

    return 0;
}

Output:

1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1

 

Optimize method to reverse elements of an array:

There are many ways to reverse the elements of a given array. The below program is also a popular way to reverse the array. Here we only need to iterate the loop N/2, where N is array size.

#include <stdio.h>


//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


//Function to reverse arr[] from start to end
void revereseArray(int arr[], int size)
{
    int i = 0;

    for (i = 0; i < size/2; i++)
    {
        int temp = arr[i];
        arr[i] = arr[size - 1 - i];
        arr[size - 1 - i] = temp;
    }
}

//print the array
void printArray(int arr[], int size)
{
    int i =0;
    for (i=0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}


int main()
{
    // Given array
    int arr[] = {1, 2, 3, 4, 5, 6};
    // length of the array
    int N = ARRAY_SIZE(arr);

    //print array elements
    printArray(arr, N);

    //reverse the array elements
    revereseArray(arr, N);

    printf("Reversed array is \n");
    //print array elements
    printArray(arr, N);

    return 0;
}

Output:

1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1