This post covers array reversal meaning reversing all the elements of an array. The left-most element will be placed on the right-most part and vice versa. The left elements will be placed to the right and vice versa.
Algorithm
STEP 1: START
STEP 2: FOR( I=1 TO [N/2])
STEP 3: SWAP(A[[I]=A[N-1-1])
STEP 4: ENDFOR
STEP 5: STOP
Code
#include <stdio.h>
int a[5];
int reverse();
int main()
{
printf("enter the elements");
for(int i=0;i<5;i++)
{ printf("a[%d]:",i);
scanf("%d",&a[i]);
}
reverse();
printf("REVERSED ARRAY:");
for(int i=0;i<5;i++)
{
printf("%d ",a[i]);
}
return 0;
}
int reverse()
{
for(int i=0;i<(5/2);i++)
{
int temp =a[i];
a[i]=a[4-i];
a[4-i]=temp;
}
}
Output
user@computer$ : enter the elements a[0]:1
a[1]:2
a[2]:3
a[3]:4
a[4]:5
REVERSED ARRAY:5 4 3 2 1
Click here to get the algorithm and the code on how to merge two arrays.
Click here to get the algorithm and the code on how to perform matrix multiplication using arrays.
Pingback: Algorithm and Code for Array Searching (Linear Search) - Ultimate Coder