# C PROGRAMS
Array Programs
➤ Find Length of Array
➤ How to Print an Array
➤ Sum of Array Elements
➤ Reverse an Array in C
➤ Copy an Array in C
➤ Merge Two Arrays in C
➤ Merge Two Sorted Arrays
➤ Count Repeated Elements
➤ Find Duplicate Elements
➤ Linear Search in C
➤ Binary Search in C
➤ Insert Element in Array
➤ Delete Element in Array
➤ Display odd-even in Array
➤ Sum & Count of Odd-Even
➤ Count +ve, -ve, & 0
➤ Sum of +ve, -ve Numbers
➤ Avg & Numbers > Avg
➤ Smallest & Largest Element
➤ 1st 2nd Max-Min
➤ Sort Array in C
➤ Search element in Array
➤ Search index of Nth times occurred element
➤ Matrix Programs
Reverse Array in C | Program description:- Write a C program to find the reverse of an array. To find the reverse of an array, we can take the help of another array or we can reverse in the same array.
Reverse an Array in C using another array
Procedure to reverse an array using another array
a) Take an array, assume realArray
b) Find the length of the original array. See:- How to find the length of an array in C
c) Declare another array with the same length, reverseArr
d) From realArray, select from the last and insert to reverseArr from the start
e) Repeat (d) until the end of the array
int arr[] = {10, 20, 30}; // original array
int reverseArr[3]; // new array
Then,
reverseArr[0] = arr[2];
reverseArr[1] = arr[1];
reverseArr[2] = arr[0];
Finally, the new array will become,
reverseArr = {30, 20, 10};
C Program to reverse an array using another array
#include <stdio.h>
int main()
{
// original array
int realArr[] = {15,25,35,40,50};
// calculate length
int length = sizeof(realArr)/sizeof(int);
// declare new array
int reverseArr[length];
// reverse the array
int i = 0;
int j = length-1;
// loop
while(j>=0) {
reverseArr[i] = realArr[j];
// update i & j
i++; // increase i
j--; // decrease j
}
// display original array
printf("Original array: \n");
for (int i = 0; i < length; ++i)
{
printf("%d ", realArr[i]);
}
// display new array
printf("\nReverse of array: \n");
for (int i = 0; i < length; ++i)
{
printf("%d ", reverseArr[i]);
}
return 0;
}
Output:-
Original array:
15 25 35 40 50
Reverse of array:
50 40 35 25 15
Instead of using while loop, we can also use for loop, but it might confuse beginners. Learn more about:- for loop in C
// reverse the array
for (int i = 0, j=length-1; j>=0; i++, j--)
{
reverseArr[i] = realArr[j];
}
Using the Same Array
Procedure to reverse an array using the same array,
a) Take an array, assume arr
b) Find the length of the array
c) Select the first and last element of the array and swap them
d) Repeat this process until length/2
If the length of the array is even then all elements of the array must swap with a relative element, but if the length of the array is odd then the element at the center position will remain the same.
Example using array of Even length,
arr[]
= {10,20,30,40}; // original array
Now, swap arr[0] and arr[3] then
arr[]
= {40,20,30,10};
Again, swap arr[1] and arr[2] then
arr[]
= {40,30,20,10; // final result
Example using array of Odd length,
arr[]
= {10,20,30,40,50}; // original array
Now, swap arr[0] and arr[4] then
arr[]
= {50,20,30,40,10};
Again, swap arr[1] and arr[2] then
arr[]
= {50,40,30,20,10}; // final result
No need to do anything with element at center position.
#include <stdio.h>
int main()
{
// original array
int arr[] = {15,25,35,40,50};
// calculate length of array
int length = sizeof(arr)/sizeof(int);
// display original array
printf("Original array: \n");
for (int i = 0; i < length; ++i)
{
printf("%d ", arr[i]);
}
// variables
int i = 0;
int j = length-1;
int temp;
// reverse the array
// loop
while(i <= length/2) {
// swap
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// update i & j
i++; // increase i
j--; // decrease j
}
// display new array
printf("\nReverse of array: \n");
for (int i = 0; i < length; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:-
Original array:
15 25 35 40 50
Reverse of array:
50 40 35 25 15
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!