Find reverse of an array in C using functions
Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in the array in reverse. (Don't print in reverse order, reverse the elements). So, if the input is: n = 6; arr = [2, 9, 1, 3, 5, 7] the output is [7, 5, 3, 1, 9, 2]
solve
Do this Follow these steps to –
For initialize i := 0, if i < Quotient of n/2:
temp := arr[i]
arr[i] := arr[n - i - 1]
arr[n - i - 1] := temp
For initialize i := 0, if i < n:
display arr[i]
If there is four array like:
arr [0] = 5
arr [1] = 4
arr [2] = 3
arr [3] = 2
After reversing the array is:
arr [0] = 2
arr [1] = 3
arr [2] = 4
arr [3] = 5
In C, you can reverse arrays in four different ways: for loops, pointers, recursion, or writing functions.
Example 1: Here, we give an example of finding the reverse of an array in C using the function given below.
#include < stdio.h >
#include < stdlib.h >
#include < conio.h >
#include < math.h >
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 ( " Enter the size of the array : " ) ;
scanf ( " % d " , & size ) ;
printf ( " Enter the elements of the array is : " ) ;
for ( i = 0 ; i < size ; i + + )
scanf ( " % d " , & arr [ i ] ) ;
printf ( " After reversing the array is : " ) ;
for ( i = 0 ; i < size ; i + + )
{
printf ( " % d " , arr [ i ] ) ;
}
return 0;
}
Output: Now we compile the program and also run it. After compilation, the result is found, which is given below.
Enter the size of the array: 4
Enter the elements of the array is: 5 6 7 8
After reversing the array is: 8 7 6 5
Explanation of the above program: The example C program above showed how to use a function to reverse an array. I created a custom inverse function that takes the array and the size of the array as parameters and created a tmp variable to swap the numbers.
Example 2: Now, here we give another example of finding the reverse of an array in C using the function given below.
#include <stdio.h>
#include <stdlib.h>
#define n 6
int main()
{
int arr[n] = {5, 6, 8, 3, 1, 2};
int temp;
for (i=0; i<n/2; i++)
{
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
for (i=0; i<n; i++)
{
Printf(“%d”, arr[i]);
}
}
Output: Now we compile the program and also run it. After compilation, the result is found, which is given below.
2, 1, 3, 8, 6, 5
Explanation of the above program: I created a custom inverse function that takes the array and the size of the array as parameters and created a tmp variable to swap the numbers.
Example 3: Now, here we give another example of finding the reverse of an array in C using the function given below.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void print (int arr[], int n)
{
for (int i = 0; i < n; i++) {
printf ("%d ", arr[i]);
}
} void reverse (int arr[], int i, int n)
{
if (i >= n) {
return;
}
int value = arr[i];
reverse (arr, i + 1, n);
arr[n - i - 1] = value;
}
int main(void)
{
int arr[] = {6, 8, 3, 1, 2};
int n = sizeof(arr)/sizeof(arr[0]);
reverse(arr, 0, n);
print(arr, n);
return 0;
}
Output: Now we compile the program and also run it. After compilation, the result is found, which is given below.
2, 1, 3, 8, 6
Explanation of the above program: The other is a repeat system that pushes items onto the call stack before returning the items to the same item in the correct order at the end of the repeat.