arr [ 1 ] = 2
arr [ 2 ] = 3
Then, by reversing the array we will have:
arr [ 0 ] = 3
arr [ 1 ] = 2
arr [ 2 ] = 1
In C, you may reverse an array in four different ways: with a for loop, pointers, recursion, or by writing a function.
Example:
include < stdio.h >
include < stdlib >
include < conio.h >
include < math >
// function to reverse an Array
void reverse ( int arr [ 10 ] , int n )
{
int i , tmp ;
for( i = 0 ; i < n / 2 ; i + + )
{
tmp = arr [ i ] ;
arr [ i ] = arr [ n - 1 - i ] ;
arr [ n - 1 - i ] = tmp ;
}
}
int main
{
int arr [ 100 ] , i , size ;
printf ( " ?nter size of array : " ) ;
scanf ( " % d " , & size ) ;
printf ( " ?nter the elements of the array : " ) ;
for ( i = 0 ; i < size ; i + + )
scanf ( " % d " , & arr [ i ] ) ;
// call reverse function
reverse ( arr , size ) ;
// display reversed array
printf ( " After reversing the array : " ) ;
for ( i = 0 ; i < size ; i + + )
{
printf ( " % d " , arr [ i ] ) ;
}
return 0 ;
}
Output:
?nter size of array: 3
?nter the elements of the array: 1 2 3
After reversing the array: 3 2 1
....................................
Process executed in 1.22 seconds
Press any key to continue.
Explanation
In the above example of a program in C, we have demonstrated how we can use function to reverse an array. We have created a user defined function reverse which takes the array and the size of the array as parameter and creates a tmp variable to swap the numbers.
In-Place Implementation
include < stdio.h >
include < stdlib >
include < conio.h >
include < math >
// Function to print contents of an array
void print ( int arr , int n )
{
for ( int i = 0 ; i < n ; i + + ) {
printf ( " % d " , arr [ i ] ) ;
}
}
// Function to reverse elements of an array
void reverse ( int arr , int n )
{
for ( int low = 0 , high = n - 1 ; low < high ; low + + , high - - )
{
int temp = arr [ low ] ;
arr [ low ] = arr [ high ] ;
arr [ high ] = temp ;
}
}
int main ( void )
{
int arr = { 1 , 2 , 3 , 4 , 5 } ;
int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ;
reverse ( arr , n ) ;
print ( arr , n ) ;
return 0 ;
}
Output:
5 4 3 2 1
.............
Process executed in 2.12 seconds
Press any key to continue.
Explanation
In the above example of a program in C, reading the items from both ends of the array and swapping them can be used to build a linear in-place algorithm, as shown in the example.
Another Example:
include <stdio.h>
// Function to print contents of an array
void print(int arr, int n)
{
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
// Function to reverse elements of an array
void reverse(int arr, int i, int n)
{
// base case: end of an array is reached or array index out-of-bounds
if (i >= n) {
return;
}
// store the next array element
int value = arr[i];
// reach the end of the array using recursion
reverse(arr, i + 1, n);
// put elements in the call stack back into the array
// in the correct order
arr[n - i - 1] = value;
}
int main(void)
{
int arr = { 1, 2, 3, 4, 5 };
int n = sizeof(arr)/sizeof(arr[0]);
reverse(arr, 0, n);
print(arr, n);
return 0;
}
Output:
5 4 3 2 1
....................
Process executed in 1.22 seconds
Press any key to continue.
Explanation
Another is a recurring system that puts the elements in a call stack before returning them to the same members in the correct order at the end of the repetition.