×

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.


Related Topics

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.