Program to print reverse array in C


Advertisements

To print an array in reverse order, we shall know the length of the array in advance. Then we can start an iteration from length value of array to zero and in each iteration we can print value of array index. This array index should be derived directly from iteration itself.

Algorithm

Let's first see what should be the step-by-step procedure of this program −

START
   Step 1 → Take an array A and define its values
   Step 2 → Loop for each value of A in reverse order 
   Step 3 → Display A[n] where n is the value of current iteration
STOP

Pseudocode

Let's now see the pseudocode of this algorithm −

procedure print_array(A)

   FOR from array_length(A) to 0
      DISPLAY A[n]
   END FOR
   
end procedure

Implementation

The implementation of the above derived pseudocode is as follows −

#include <stdio.h>

int main() {
   int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
   int loop;

   for(loop = 9; loop >= 0; loop--)
      printf("%d ", array[loop]);
      
   return 0;
}

The output should look like this −

0 9 8 7 6 5 4 3 2 1
array_examples_in_c.htm
Advertisements