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 and arr 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 and revIndex = 0.
Run loop from size – 1 to 0 in decremented style. The loop structure should look like while(arrIndex >= 0).
Inside loop copy original array to reverse array i.e. reverse [revIndex] = arr[arrIndex];.
After copy, increment revIndex and decrement arrIndex.
Finally after loop print reverse array.
Program to find reverse of array
* C program to find reverse of array
#define MAX_SIZE 100 // Maximum array size
int arr[MAX_SIZE], reverse[MAX_SIZE];
int size, i, arrIndex, revIndex;
/* Input size of the array */
printf(“Enter size of the array: “);
/* Input array elements */
printf(“Enter elements in array: “);
for(i=0; i= 0)
/* Copy value from original array to reverse array */
reverse[revIndex] = arr[arrIndex];
* Print the reversed array
printf(“\nReversed array : “);