This part will cover various methods to invert an array in the C++ programming language. Reversing an array involves altering the sequence of elements within the array. This approach flips the final element of the array to the beginning, making the initial element the new last one. This sequence is repeated until all elements or characters within the array are entirely reversed.
For instance, if we have an array with elements such as 'H', 'E', 'L', 'L', 'O', reversing all the elements in the array would result in the array being transformed into 'O', 'L', 'L', 'E', 'H'. Thus, the reversal process effectively flips all the characters within the array.
Different ways to reverse an array
Following are the various ways to get the reverse array in the C++ programming language.
- Reverse an array using for loop
- Reverse an array using the reverse function
- Reverse an array using the user-defined function
- Reverse an array using the pointers
- Reverse an array using the Recursion function
Program to reverse an array using the for loop
Let's develop a program in C++ to invert the elements of an array by employing a for loop.
Program1.cpp
#include <iostream>
using namespace std;
int main ()
{
int arr[50], num, temp, i, j;
cout << " Please, enter the total no. you want to enter: ";
cin >> num;
// use for loop to enter the numbers
for (i = 0; i < num; i++)
{
cout << " Enter the element " << i+1 << ": ";
cin >> arr[i];
}
for ( i = 0, j = num - 1; i < num/2; i++, j--)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
cout << "\n Reverse all elements of the array: " << endl;
// use for loop to print the reverse array
for ( i = 0; i < num; i++)
{
cout << arr[i] << " ";
}
return 0;
}
Output
Please, enter the total no. you want to enter: 6
Enter the element 1: 78
Enter the element 2: 12
Enter the element 3: 54
Enter the element 4: 24
Enter the element 5: 7
Enter the element 6: 90
Reverse all elements of the array:
90 7 24 54 12 78
Program to reverse an array using the reverse function
Let's explore an illustration demonstrating how to output the inverse of an array by utilizing the reverse method in C++.
Program2.cpp
#include <iostream>
#include <algorithm>
using namespace std;
// declare disp() function
void disp(int arr1[], int num)
{
int i;
// use for loop to iterate the characters
for ( i = 0; i < num; i++)
{
cout << arr1[i] << " ";
}
}
// define reverse() function to reverse the array elements
void reverse(int arr1[], int num)
{
reverse(arr1, arr1 + num);
}
int main ()
{
// declare and initialize an array
int arr1[] = {34, 78, 21, 90, 5, 2};
int num = sizeof(arr1)/sizeof(arr1[0]);
// call reverse function and pass parameters
reverse(arr1, num);
disp(arr1, num); /* call disp() function to print the revrse array. */
return 0;
}
Output
2 5 90 21 78 34
Program to reverse an array using the user-defined function
Let's explore an illustration demonstrating the reversal of array elements utilizing a custom function in C++.
Program3.cpp
#include <iostream>
using namespace std;
void ArrRev ( int [], int);
int main ()
{
int arr[50], num, i, j, temp;
cout << " Number of elements to be entered: " << endl;
cin >> num;
cout << " Enter the array elements: " << endl;
// use for loop to enter the elements
for ( i = 0; i < num; i++)
{
cin >> arr[i];
}
cout << " Elements are: \n";
// display entered elements in array
for ( i = 0; i < num; i++)
{
cout << arr[i] << " ";
}
ArrRev (arr, num); // call function
cout << " \n The reverse of the given array is: \n";
// use for loop to print the reverse array elements
for ( i = 0; i < num ; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
void ArrRev ( int ar[], int a2)
{
int i, j, temp;
j = a2 - 1;
for ( i = 0; i < j; i++, j--)
{
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
Output
Number of elements to be entered:
7
Enter the array elements:
45
32
89
21
78
34
65
Elements are:
45 32 89 21 78 34 65
The reverse of the given array is:
65 34 78 21 89 32 45
Program to reverse an array using the pointer
Let's explore an illustration to showcase the reversal of array elements utilizing pointers in C++.
Program4.cpp
#include <iostream>
using namespace std;
int main ()
{
// declare the size of array
int arr[50], arr2[50];
int *ptr, i, num;
cout <<" No. of array elements to be entered: "<< endl;
cin >> num;
cout <<" Enter the elements: ";
// use for loop to insert the array elements
for (i = 0; i < num; i++)
{
cin >> arr[i];
}
ptr = &arr[0];
cout <<" Entered elements of the array are: \n" <<endl;
for (i = 0; i < num; i++)
{
cout << "\t" << *ptr;
ptr++;
}
ptr--; // decrement ptr
for ( i = 0; i < num; i++)
{
arr2[i] = *ptr;
ptr--;
}
ptr = &arr2[0];
for ( i = 0; i < num; i++)
{
arr[i] = *ptr;
ptr++; // increment ptr
}
ptr = &arr[0]; // ptr hold the base address of arr[0]
cout <<" \n The reversed array elements are: \n " << endl;
// print the array elements using ptr
for ( i = 0; i < num; i++)
{
cout <<" \t " << *ptr <<endl;
ptr++;
}
return 0;
}
Output
No. of array elements to be entered:
6
Enter the elements: 45
32
89
63
4
6
Entered elements of the array are:
45 32 89 63 4 6
The reversed array elements are:
6
4
63
89
32
45
Reverse an array using the Recursion function
Let's develop a C++ program that utilizes a recursive function to reverse the elements of an array.
Program5.cpp
#include <iostream>
using namespace std;
// initialize array
int arr[] = { 20, 34, 5, 8, 1, 78};
// size of the array
int size = sizeof( arr) / sizeof (arr[0]);
void reverseArr ( int arr[] , int num)
{
// check the size of array
if ( num == size)
return;
// extract array elements
int elem = arr [ num];
// recursively calls the next element of the array
reverseArr ( arr, num + 1);
// assigning elements
arr [ size - num - 1] = elem;
}
int main ()
{
int i;
// call recursive function (start from first elements
cout << " Original elements of the arrays " << endl;
for ( int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
reverseArr (arr, 0);
cout << " \n Reverse elements of the array are: " << endl;
// display the array elements
for ( int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
return 0;
}
Output
Original elements of the arrays
20 34 5 8 1 78
Reverse elements of the array are:
78 1 8 5 34 20