Reverse the array using another array
C program to reverse the array using another array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void print_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:%d\n",i+1,a[i]);
}
}
void rev_array(int a[MAX], int b[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
b[i] = a[n-i-1];
}
}
void main()
{
int arr[MAX], rev_arr[MAX];
int i, n;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of first array\n");
read_array(arr, n);
rev_array(arr, rev_arr, n);
printf("\nArray elements after reversing\n");
print_array(rev_arr, n);
getch();
}
Output
Enter the number of elements in the array :5
Enter the elements of first array
Element 1 :1
Element 2 :2
Element 3 :3
Element 4 :4
Element 5 :5
Array elements after reversing
Element 1 :5
Element 2 :4
Element 3 :3
Element 4 :2
Element 5 :1