Q:

Sort an array in descending order using sort() function in C++ STL

belongs to collection: C++ programs on various topics

0

Example:

    Input:
    Array: 10, 1, 20, 2, 30

    Output:
    Sorted Array: 30, 20, 10, 2, 1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ program:

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	//declare and define an array
	int arr[]={10, 1, 20, 2, 30};
	
	//size of the array
	//total size/size of an element
	int size = sizeof(arr)/sizeof(int);
	
	//calling sort() to sort array elements
	sort(arr, arr+5, greater<>());
	
	//printing sorted elements
	cout<<"Sorted elements are: ";
	for(int i=0; i<size; i++)
		cout<<arr[i]<<" ";
	
	return 0;
}

Output

 
Sorted elements are: 30 20 10 2 1 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Binary Search in C++ using Standard Template Libra... >>
<< Sort an array in ascending order using sort() func...