In this blog post, we learn how to write a C Program to reverse the elements of an array? So here will write the C Program to reverse the elements of an array. We will also see how to display array elements in reverse order.
So let’s see the logic to reverse the array elements. Suppose arr is an integer array of size N (arr[N] ), the task is to write the C Program to reverse the elements of the given array.
Examples:
//Input array Input : arr[] = {1, 2, 3} //output array Output : arr[] = {3, 2, 1}
Logic to reverse the elements of an array:
1. Create two intermediate variables start and end.
2. Initialize the start and end variable with 0 and n-1 (where n is the number of elements in the array).
3. Now In the loop, swap arr[start] with arr[end] and update the value of start and end variables as follows :
//Update start start = start +1; //update end end = end – 1;
C Program to reverse the elements of an array:
#include<stdio.h> //Calculate array size #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) int main() { int arr[] = {1,2,3,4,5,6}; // length of the array int N = ARRAY_SIZE(arr); // assign the 0 valid index int start = 0; // assign the last valid index int end = (N - 1); int tmp, i; while(start < end) { // swap the elements tmp = arr[start]; arr[start] = arr[end]; arr[end] = tmp; start++; end--; } // print the reversed array for( i = 0; i < N; i++) { printf("%d ", arr[i]); } return 0; }
Output:
How does it work?
Recursive method to reverse the elements of an array:
We can also use the recursive method to reverse the elements of an array. If you don’t know about the recursive method, you can read this article “How to use the recursive function in C?”.
#include <stdio.h> //Calculate array size #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) //Function to reverse arr[] from start to end void rvereseArray(int arr[], int start, int end) { int temp; if (start >= end) { return; } //swap array element temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; rvereseArray(arr, start+1, end-1); } //print the array void printArray(int arr[], int size) { int i =0; for (i=0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { // Given array int arr[] = {1, 2, 3, 4, 5, 6}; // length of the array int N = ARRAY_SIZE(arr); int startIndex =0; int endIndex = N-1; //print array elements printArray(arr, N); //reverse the array elements rvereseArray(arr, startIndex, endIndex); printf("Reversed array is \n"); //print array elements printArray(arr, N); return 0; }
Output:
1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1
Optimize method to reverse elements of an array:
There are many ways to reverse the elements of a given array. The below program is also a popular way to reverse the array. Here we only need to iterate the loop N/2, where N is array size.
#include <stdio.h> //Calculate array size #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) //Function to reverse arr[] from start to end void revereseArray(int arr[], int size) { int i = 0; for (i = 0; i < size/2; i++) { int temp = arr[i]; arr[i] = arr[size - 1 - i]; arr[size - 1 - i] = temp; } } //print the array void printArray(int arr[], int size) { int i =0; for (i=0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { // Given array int arr[] = {1, 2, 3, 4, 5, 6}; // length of the array int N = ARRAY_SIZE(arr); //print array elements printArray(arr, N); //reverse the array elements revereseArray(arr, N); printf("Reversed array is \n"); //print array elements printArray(arr, N); return 0; }
Output:
1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1
Recommended Articles for you:
- Best gift for programmers.
- Best electronic kits for programmers.
- C Program to find the maximum and minimum element in the array
- How to create a dynamic array in C?
- How to access 2d array in C?
- A brief description of the pointer in C.
- Dangling, Void, Null and Wild Pointers
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- Pointer Arithmetic in C.
- void pointer in C.
- 10 questions about dynamic memory allocation.
- Memory Layout in C.
- 100 C interview Questions
- File handling in C.
- C format specifiers.