C program to find the reverse of an array – In this article, we will explain the many means to find the reverse of an array in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this piece are as follows:
- Using Standard Method
- Using Function
As it is very well known, an array is a sequential arrangement of a bunch of elements in a horizontal fashion. The locations of the elements of an array are defined by pointers.
Firstly, as per the given example above, you need to specify the size of the array.
In this case, the size of the array in the example is chosen to be 5.
Then, you need to enter the elements into the array.
The elements entered in this array are as follows:
1 2 3 4 5
The array after reversing the elements becomes like this:
5 4 3 2 1
Thus, the many ways to do the same in C programming are as follows:
Using Standard Method
- Read the size of the array and initialize to the variable n.
2) scanf() function reads the entered element and will assign the element to a[i] as scanf(“%d”,&a[i]) using for loop for(i=0;i<n;i++).
3) The main() function calls the print functoion by passing array a,size of the array as arguments. The print() function prints the arrat elements using for loop for(i=0;i<n;i++) as prinf(“%d”,a[i]).
4) To reverse the array element
for loop from i=0 to i<n/2
a) assign the first element with index ‘i’ to temp.
b) assign last element with index [n-i-1] to first element with index ‘i’.
c) assign temp to last element with index [n-i-1]. Repeat these a,b,c steps by increasing i value until i<n/2.
In the above steps, we interchanged the first half of the array elements with 2nd half array elements from the last index.
5) After reversing the given array main() calls the print() function to print the reverse array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <stdio.h> #include <conio.h> print(int *a,int n) { int i; for(i=0; i<n; i++) { printf("%d ",a[i]); } } int main() { int a[10000],i,n,temp ; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("\n before reverse \n"); print(a,n); for(i=0; i<n/2; i++) { temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp; } printf(" \nafter reverse \n"); print(a,n); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter size of the array: 5 Enter elements in array: 1 2 3 4 5 before reverse 1 2 3 4 5 after reverse 5 4 3 2 1 |
Using Function
- The function reverse() contains the code to reverse the given array.
2) The main() function calls the print() function before reversing the array. The function print() prints the given array element using printf statement and the for loop.
3) The main() function calls the reverse() function by passing the given array, size of the array as arguments. The reverse() function
The reverse() function,
a) Assign the 1st element to temp,
b) Assign the last element to 1st and
c) The temp is assigned to the last element
until all iterations of for loop for(i=0;i<n/2;i++).
4) After all iterations of for loop, we will get the reverse array. The main() function calls the print() function to print the reverse array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <stdio.h> #include <conio.h> reverse(int *a,int n) { int i,temp; for(i=0; i<n/2; i++) { temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp; } } print(int *a,int n) { int i; for(i=0; i<n; i++) { printf("%d ",a[i]); } } int main() { int a[10000],i,n; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("\n before reverse \n"); print(a,n); reverse(a,n); printf(" \nafter reverse \n"); print(a,n); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter size of the array: 5 Enter elements in array: 10 9 8 7 6 before reverse 10 9 8 7 6 after reverse 6 7 8 9 10 |