Q:

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

belongs to collection: C++ programs on various topics

0

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

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);
	
	//printing sorted elements
	cout<<"Sorted elements are: ";
	for(int i=0; i<size; i++)
		cout<<arr[i]<<" ";
	
	return 0;
}

Output

 
Sorted elements are: 1 2 10 20 30 

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
Sort an array in descending order using sort() fun... >>
<< Print Reverse Triangle Bridge Pattern for Characte...