reverse order array cpp code example

Example 1: reverse sort cpp

int main(){    
  	int arr[5] = {1,3,2,4,5};
	sort(arr, arr+5, greater<int>()); 
  	// arr == {5,4,3,2,1}
  	return 0;
}

Example 2: 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 3: c++ reverse array

int arr[5] = {1, 2, 3, 4, 5}; //Initialize array

for(int i = 0; i < size(arr); i++) {
	//Create temporary variable to hold current value in array
	int temp = arr[i];
	//Set the current value in the array to the mirrored value in array
	arr[i] = arr[size(arr) - 1 - i];
	//Set mirrored value in array to temp, swapping the two numbers
	arr[size(arr) - 1 - i] = temp;
}

Tags:

Cpp Example