C program to find reverse of an array

Write a C program to read elements in an array and find its reverse. C program to print reverse of an array. How to find reverse of an array elements. Logic to find reverse of an array.

Example

Input

Input array elements: 10, 5, 16, 35, 500

Output

Array elements after reverse: 500, 35, 16, 5, 10

Required knowledge

Basic C programming, For Loop, Array

Logic to print array in reverse order

There are various ways to reverse an array. Here I will explain the basic three algorithms to reverse a given array. First the simplest and easiest one, so that ever beginner can get what I am up to.

However, this algorithm in real does not produces a reversed array. Instead it just prints the array in reverse order. If you are looking to reverse the elements then skip to next logic. So here goes formal step by step descriptive logic to print array in reverse order.

  1. Input size and elements in an array. Say size stores size of the array and array stores elements of array.
  2. Run a loop from size - 1 to 0 in decremented style. The loop structure should look like for(i=size-1; i>=0; i--).
  3. Inside the loop print the current value of array element using array[i].

Program to print array in reverse

/**
 * C program to print array in reverse order
 */

#include <stdio.h>
#define MAX_SIZE 100 // Defines maximum size of array

int main()
{
    int array[MAX_SIZE];
    int size, i;

    /* Input size of array */
    printf("Enter size of the array: ");
    scanf("%d", &size);

    /* Input array elements */
    printf("Enter elements in array: ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &array[i]);
    }

    /*
     * Print array in reversed order
     */
    printf("\nArray in reverse order: ");
    for(i = size-1; i>=0; i--)
    {
        printf("%d\t", array[i]);
    }

    return 0;
}

Logic to reverse an array

The above program just print array in reversed order. It does not reverses the array. Here I am writing the first basic logic to reverse an array. It uses above approach to access array element in reverse and copy it to a new reverse array. Which means last element of original array becomes the first element for reverse array. Below is the step by step descriptive logic to reverse a given array.

  1. Input size and elements in an array in two variables size and array respectively.
  2. Declare another array that will store the reversed elements of original array with same size. Say int reverse[size] will store the reversed array elements.
  3. Initialize two variables that will keep track of original and reverse array. Here we will access original array from last and reverse array from first. Hence, initialize arrIndex = size-1 and revIndex = 0.
  4. Run a loop from size - 1 to 0 in decremented style. The loop structure should look like while(arrIndex >= 0).
  5. Inside the loop copy original array value to reverse array. Say reverse [revIndex] = array[arrIndex];.
  6. After copying values increment the revIndex and decrement arrIndex.
  7. Finally after loop print the value of reverse array.

Program to reverse an array

/**
 * C program to find reverse of an array
 */

#include <stdio.h>
#define MAX_SIZE 100 // Defines maximum size of array

int main()
{
    int array[MAX_SIZE], reverse[MAX_SIZE];
    int size, i, arrIndex, revIndex;

    /* Input size of the array */
    printf("Enter size of the array: ");
    scanf("%d", &size);

    /* Input array elements */
    printf("Enter elements in array: ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &array[i]);
    }

    revIndex = 0;
    arrIndex = size - 1;
    while(arrIndex >= 0)
    {
        /* Copy value from original array to reverse array */
        reverse[revIndex] = array[arrIndex];
        
        revIndex++;
        arrIndex--;
    }

    /*
     * Print the reversed array
     */
    printf("\nReversed array : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", reverse[i]);
    }

    return 0;
}

The above method is easy to write and understand for beginners. However, unknowingly we are wasting some memory to store reverse array. You can also reverse the given array without using another array.

Logic to reverse an array without using another array

Logic to reverse array without using another array relies on the above logic. What we need to do is maintain two array indexes. First arrIndex that moves from size - 1 to 0. Second revIndex that moves from 0 to size - 1. Now instead of copying values to a reverse array, swap values of array at arrIndex and revIndex indexes. This will reverse the entire array.

Important note: While swapping array elements do ensure that revIndex must not cross arrIndex.

Read more - Program to swap two numbers

Program to reverse an array without using another array

/**
 * C program to reverse an array without using second array
 */

#include <stdio.h>
#define MAX_SIZE 100 // Defines maximum size of array

int main()
{
    int array[MAX_SIZE];
    int size, i, arrIndex, revIndex;
    int temp; // Used for swapping 

    /* Input size of the array */
    printf("Enter size of the array: ");
    scanf("%d", &size);

    /* Input array elements */
    printf("Enter elements in array: ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &array[i]);
    }

    revIndex = 0;
    arrIndex = size - 1;
    while(revIndex < arrIndex)
    {
        /* Copy value from original array to reverse array */
        temp = array[revIndex];
        array[revIndex] = array[arrIndex];
        array[arrIndex] = temp;
        
        revIndex++;
        arrIndex--;
    }

    /*
     * Print the reversed array
     */
    printf("\nReversed array : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", array[i]);
    }

    return 0;
}

Read more - Program to find reverse of a number

Output

Enter size of the array: 5
Enter elements in array: 10 5 16 35 500

Reversed array : 500      35      16      5      10

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>