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

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, arr+n); 
    int a;
    for ( a = 0; a< n; a++) 
        cout << arr[a] << " ";  
    return 0; 
}
A. 1 2 3 4 5
B. 1 3 4 2 5
C. 5 4 3 2 1
D. error
 
Answer: a
Explanation: The given program sorts the input in ascending order. Function sort() uses two parameters in the form of address of the first and last element of the array to sort the array.
 

Tìm kiếm

Back
Top