Write a C program to input elements in array and find reverse of array. How to find reverse of array in C programming. Logic to find reverse of array in C program.
Example
Input
Input array elements: 10, 5, 16, 35, 500
Output
Array elements after reverse: 500, 35, 16, 5, 10
Required knowledge
Basic Input Output, For loop, While loop, Array
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.
Logic to print array in reverse order
This algorithm in real does not produces a reversed array. Instead it just prints array in reverse order. If you are looking to reverse the elements then skip to next logic. So here goes step by step descriptive logic to print array in reverse order.
- Input size and elements in array from user. Store it in some variable say
size
andarr
. - Run a loop from
size - 1
to 0 in decremented style. The loop structure should look likefor(i=size-1; i>=0; i--)
. - Inside loop print current array element i.e.
arr[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 arr[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", &arr[i]);
}
/*
* Print array in reversed order
*/
printf("\nArray in reverse order: ");
for(i = size-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}
return 0;
}
Logic to find reverse of array
The above program prints array in reversed order. It does not reverses array. Here I am writing first basic logic to reverse 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.
Step by step descriptive logic to reverse an array.
- Input size and elements in an array. Store it in some variable say
size
andarr
respectively. - Declare another array that will store reversed array elements of original array with same size, say
reverse[size]
. - 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
andrevIndex = 0
. - Run loop from
size - 1
to 0 in decremented style. The loop structure should look likewhile(arrIndex >= 0)
. - Inside loop copy original array to reverse array i.e.
reverse [revIndex] = arr[arrIndex];
. - After copy, increment
revIndex
and decrementarrIndex
. - Finally after loop print
reverse
array.
Program to find reverse of array
/**
* C program to find reverse of array
*/
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[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", &arr[i]);
}
revIndex = 0;
arrIndex = size - 1;
while(arrIndex >= 0)
{
/* Copy value from original array to reverse array */
reverse[revIndex] = arr[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 array without using another array
Logic to reverse array without using another array relies on 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 value 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
.
Learn more how to reverse array using pointers.
Program to reverse array without using another array
/**
* C program to reverse an array without using second array
*/
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[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", &arr[i]);
}
revIndex = 0;
arrIndex = size - 1;
while(revIndex < arrIndex)
{
/* Copy value from original array to reverse array */
temp = arr[revIndex];
arr[revIndex] = arr[arrIndex];
arr[arrIndex] = temp;
revIndex++;
arrIndex--;
}
/*
* Print reversed array
*/
printf("\nReversed array : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
Learn more about program to find reverse of 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 😉