×

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

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes 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.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

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.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.