Data structures in Python and C , Series 5 : Reversing a array or list
C program to reverse an array
C program to reverse an array: This program reverses the array elements. For example, if 'A' is an array of integers with three elements such thatA[0] = 1, A[1] = 2, A[2] = 3
Then after reversing, the array will be
A[0] = 3, A[1] = 2, A[0] = 1
Given below is the C code to reverse an array.
#include<stdio.h>
int main()
{
int array[100],n,c,temp;
printf("enter the number of elements");
scanf("%d",&n);
for(c = 0;c <n;c++)
{
printf("enter the %d element",c);
scanf("%d",&array[c]);
}
printf("now we are going to reverse the array by swapping");
for(c =0; c<n/2 ;c++)
{
temp = array[c];
array[c] = array[n-1-c];
array[n-1 - c] = temp;
}
printf("the reversed array is ");
for(c=0;c<n;c++)
{
printf("%d \n",array[c]);
}
return 0;
}
Python program to reverse an array:
Its very simple just if a list is :a = [1,2,3,4,5]
a_rev = a[::-1]
Comments
Post a Comment