What will be the output of the given C++ code? [code] #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3,4,2,5};

Thành Nguyễn

New member
What will be the output of the given C++ code?
Code:
#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
	int arr[] = {1, 3,4,2,5}; 
	int n = sizeof(arr)/sizeof(arr[0]);
	sort(arr+2, arr+n, greater<int>()); 
        int a;
	for (int a = 0; a < n; a++) 
		cout << arr[a] << " "; 
	return 0; 
}
A. 1 2 3 4 5
B. 1 5 4 3 2
C. 5 4 3 2 1
D. 1 3 5 4 2
 
Answer: d
Explanation: As the first parameter to function sort() is arr+2 so the sorting begins from the third element i.e. 4. Also as there is a third argument greater () to the function sort() so the sorting will be done in descending order.
 

Tìm kiếm

Back
Top