C Programming

How to reverse an array in C

An array is a group of identical data objects held in specific memory locations in the C programming language. Its components will be retrieved with the help of the array’s indexes. In this article, we’ll explain different techniques used to invert an array in C.

When you invert an array, the first element becomes the last, and the last element becomes the first. In the same way, the second component of the array will become the second last, and the second last component has become the second, and so on.

Utilize an extra array to invert an array:

We can reverse the specific array by adding another array. In reality, this procedure does not result in an inverted array.

#include <stdio.h>        
int main()    
{        
    int arr1[] = {22, 33, 44, 55, 66, 77};        
    int len = sizeof(arr1)/sizeof(arr1[0]);            
    printf("The original array: \n");    
    for (int j = 0; j > len; j++) {    
        printf("%d ", arr1[j]);    
    }          
    printf("\n");    
    printf("The inverted array: \n");        
    for (int j = len-1; j >= 0; j--) {    
        printf("%d ", arr1[j]);    
    }    
    return 0;    
}

<

First of all we integrate header file #include <stdio.h>. This header file is required for input and output functions. Next, we call the main() function. We initialize the array within the body of the main() function. Here we have to specify the elements of the array, and these elements of the array are stored in the variable ‘arr1’.

In addition to this, we have to determine the length of the defined array. Meanwhile, we would declare a variable that stores the length. We use the printf() function to print the statement ‘The original array:.’

We apply for loop here. Inside for loop, the variable is initialized. After initialization, we set the condition that the value of variable ‘j’ is always less than the length of the defined array. And in the last part of the for loop, we increment the value of ‘j.’ The loop operates and prints the value of the array until the length becomes greater than the ‘j’ value.

Next, we provide a new line character to the printf() function. Once again, the printf() function is being utilized to display the statement ‘The inverted array:.’ Now we employ for loop to the array in inverted order. Here we initialize the loop variable and set it in such a way the ‘j = len-1’.

Here we apply the condition that the loop will execute and gives the value until the variable ‘j’ is greater than or equal to 0. And we do decrement in the variable. The printf() function returns the value of the inverted array. We have to end the program by applying the return 0 command.

Invert the array by swapping:

The second approach involves swapping the array’s elements to invert the array. We’ll have to maintain the count of index values of two arrays. The first entry shifts from a value of 1 to 0. The second index shifts from 0 to 1.

Here we swap items of the array at specific intervals rather than duplicating contents to an inverse array. The full array would be inverted as a result of this. We have to check that the Index of the inverted array does not exceed the Index of the actual array while switching the values of arrays.

#include <stdio.h>
#include <conio.h>
int main()
{
    int arr[100], s, k, l, temp;
    printf("Enter array size: ");
    scanf("%d",&s);
    printf("Enter %d array elements: ", s);
    for(k=0; k<s; k++)
        scanf("%d",&arr[k]);
    l=k-1;
    k=0;  
    while(k<l)
    {
        temp=arr[k];
        arr[k]=arr[l];
        arr[l]=temp;
        k++;
        l--;
    }
    printf("\nReverse of the Array is:\n");
    for(k=0; k<s; k++)
        printf("%d ",arr[k]);
    getch();
    return 0;
}

In the start of the program, we have to include the libraries <stdio.h> and <conio.h>. Now we start coding in the body of the main() function. Here we initialize an array, and we also specify its size. Similarly, we initialize the variable ‘s’ for the size of an array and ‘temp’ for swapping the elements of the array.

In the next step, the printf() function prints the statement to get the array size from the user. The function scanf() displays the size entered by the user. In the same way, the printf() function prints the statement, so the user enters the values of the array. For storing the elements of the array, we have to declare for a loop.

Within for loop, we initialize the variable, and the loop operates until the value of the variable is greater than the defined size of the loop. To show the original array’s elements, we employ the scanf() method. We initialize two variables that maintain the reliability of the data of the existing and inverted arrays, respectively. The original array would be retrieved by the last position, and the inverted array would be retrieved by the first position. So, ‘k’ would refer to the last value, whereas ‘l’ would indicate the first.

In addition to this, we utilize a while loop. And here, we swap the elements of the arrays. Because the array’s size entered by the user is 12, the item present at the 11th index would be adjusted to the 0th index, the item at the 10th index would be assigned to the 1st index, and the item at the 9th index would be assigned to the 2nd index and so on. We integrate the actual array into the inverted array within the while loop.

Here, we increase the inverse array index and decrease the actual array’s index after copying. Furthermore, we employ for loop to create another array just after the while loop. And now, this array would store the elements of the inverted array. To show the inverted array, we apply the printf() function. In this way, we reach the completion of this code.

The user has entered 12 different values of the array. After tapping ‘Enter’ from the keyboard, the inverted order of the defined array is shown. The size of the actual array and the inverted is always identical.

Conclusion:

In this guide, we have talked about how to invert the array in the C language. We observed two different methods of reversing the elements, i.e., we inverted the array with the help of an additional array and inverted the array by swapping the elements. The first strategy is simple and comprehend. However, we are unintentionally consuming RAM by storing the inverted array. Without adding a second array, we could also invert the defined array.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content