Latest :

C Program : To Reverse the Elements of An Array | C Programs

C program to display the given array in reverse order. To do so, we will require the number of elements or size of the array and the elements or data values of the array as inputs. Our expected output as clearly mentioned in the problem statement is, the array being displayed in the reverse order.

C Program to Reverse an Array Using Function

First step is always to gather all the necessary inputs for a given problem. We can read our input at runtime using scanf() function. This is a predefined function in C which is used to read input of any primtive datatype (int, float, long, char, double, etc) at runtime. On the basis on the format specifier mentioned, the input datatype is determined.

Here, all our inputs are of integer type hence, we use the ‘%d’ format specifier to read the inputs. We first read the size of the array or number of elements in the array (n). Then, we create an array of that size and iterate through the array to read the elements or data values of the array.

int n,i;

printf(“Enter number of elements in an array : ” );

scanf(“%d”,&n);

int arr[n];

printf(“Enter %d elements :\n”,n);

for(i = 0; i < n; i++) {

scanf(“%d”,&arr[i]);

}

Now that we have all the necessary inputs, we make a function call to a user-defined function (reverse). This function takes in the input array and its size as parameters and returns void or nothing.

This function consists of the main logic in displaying the array elements in reverse order.

reverse(arr,n);

The first thing we’ll do here is, to display the array elements in normal order. We do this so that, the user has a clarity of the order of array elements first. To display in normal order, we traverse from first index (0) to the last index (n) in loop and display every element until we reach the end of array.

printf(“Array is :(“);

for(i = 0; i < n; i++) {

printf(“%d “,a[i]);

}

printf(“)”);

Now, to display in reverse order, we use the size of array (n). Since the indexing is starting from 0, the position of last element is at index (n-1 or size-1). So, we iterate in reverse order in the looping statement i.e., from ending (n-1) to the beginning (0).

For every iteration we display the value at that index using printf() function and then decrement the index for the next iteration. This continues until we reach 0th index and display the value at 0th index.

printf(“\nReverse of an array is :(“);

for(i = n-1; i >= 0; i–) {

printf(“%d “,a[i]);

}

printf(“)”);

This way the array can be displayed in reverse order using looping statements in iterative approach, Reverse array in C using pointers.

Output:

Reverse array in C using for loop

Above we have seen the use of functions and the logic to display a given array in reverse order. Using functions has two major benefits.

One is that, it makes the code reusable i.e., if same logic is required in another part of the code then, we can just make a function call and pass necessary parameters instead of rewriting it.

Another advantage is that, using functions enhances the readability of the code.

This is because, we split the code based on its functionality and logic so, everything is placed in systematic order in different function blocks thereby, making the code cleaner.

At the same time, writing the entire code within main method is also correct. Both the methods have the same time and space complexity.

If we are sure that, we do not have the necessity to use this logic elsewhere in the code then, we can just write everything within main method using the same logic as that discussed above. We can see the code below for better understanding.

Output:

x

Check Also

C Program To Check A Number Is Negative, Positive Or Zero | C Programs

C program to check whether a number is negative, positive or zero – In this ...