×

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

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 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.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 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 Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.