reverse of array in c++ code example
Example 1: reverse an array in cpp
#include<iostream>
using namespace std;
int main()
{
int n=3;
int arr[]={1,2,3,4};
while (n>=0)
{
cout<<arr[n];
n--;
}
return 0;
}
Example 2: reverse an array in c++
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 9;
int arr [SIZE];
cout << "Enter numbers: \n";
for (int i = 0; i < SIZE; i++)
cin >> arr[i];
for (int i = 0; i < SIZE; i++)
cout << arr[i] << "\t";
cout << endl;
cout << "Reversed Array:\n";
int temp, start = 0, end = SIZE-1;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for (int i = 0; i < SIZE; i++)
cout << arr[i] << "\t";
cout << endl;
return 0;
}
Example 3: c++ reverse array
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < size(arr); i++) {
int temp = arr[i];
arr[i] = arr[size(arr) - 1 - i];
arr[size(arr) - 1 - i] = temp;
}