
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Reverse an array in C++
The article showcase an array to be reversed in descending order using the C++ coding wherein the highest index is swapped to lowest index consequently by traversing the array in the loop.
Example
#include <iostream> #include <algorithm> using namespace std; void reverseArray(int arr[], int n){ for (int low = 0, high = n - 1; low < high; low++, high--){ swap(arr[low], arr[high]); } for (int i = 0; i < n; i++){ cout << arr[i] << " "; } } int main(){ int arrInput[] = { 11, 12, 13, 14, 15 }; cout<<endl<<"Array::"; for (int i = 0; i < 5; i++){ cout << arrInput[i] << " "; } int n = sizeof(arrInput)/sizeof(arrInput[0]); cout<<endl<<"Reversed::"; reverseArray(arrInput, n); return 0; }
Output
As the integer type of array supplied in a bid to be reversed in descending order, the following yields as ;
Array::11 12 13 14 15 Reversed::15 14 13 12 11
- Related Articles
- Reverse an array using C#
- C# program to reverse an array
- C program to reverse an array elements
- How to reverse an Array using STL in C++?
- C++ program to reverse an array elements (in place)
- All reverse permutations of an array using STL in C++?
- Write a program to reverse an array or string in C++
- Java program to reverse an array
- Array reverse() vs reverse! in Ruby
- How to print the elements in a reverse order from an array in C?
- How do I reverse an int array in Java
- Write a program to reverse an array in JavaScript?
- Reverse Subarray To Maximize Array Value in C++
- Array reverse() in JavaScript
- Write a Golang program to reverse an array

Advertisements