
- Learn C By Examples Time
- Learn C by Examples - Home
- C Examples - Simple Programs
- C Examples - Loops/Iterations
- C Examples - Patterns
- C Examples - Arrays
- C Examples - Strings
- C Examples - Mathematics
- C Examples - Linked List
- C Programming Useful Resources
- Learn C By Examples - Quick Guide
- Learn C By Examples - Resources
- Learn C By Examples - Discussion
Program to reverse copy array in C
This program shall help you learn one of basics of arrays. We shall copy one array into another but in reverse.
Algorithm
Let's first see what should be the step-by-step procedure of this program −
START Step 1 → Take two arrays A, B Step 2 → Store values in A Step 3 → Set count to sizeof(A) Step 4 → Loop for each value of A Step 5 → Copy A[loop] to B[count] Step 6 → Decrement count Step 7 → Display B STOP
Pseudocode
Let's now see the pseudocode of this algorithm −
procedure reversecopy_array(A, B) SET index to 1 Set count to sizeof(A) FOR EACH value in A DO B[count] = A[index] INCREMENT index DECREMENT count END FOR DISPLAY B end procedure
Implementation
The implementation of the above derived pseudocode is as follows −
#include <stdio.h> int main() { int original[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; int copied[10]; int loop, count; count = 9; for(loop = 0; loop < 10; loop++) { copied[count] = original[loop]; count--; } printf("original -> copied \n"); for(loop = 0; loop < 10; loop++) { printf(" %2d %2d\n", original[loop], copied[loop]); } return 0; }
The output should look like this −
original -> copied 1 0 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 0 1
array_examples_in_c.htm
Advertisements