Correct Answer - Option 4 : 91 81 71 61 51 41 31 21 11
The correct answer is option 4.
'arr' is an array of elements. Let us consider starting element address is 100 containing each element of 2 bytes of data.
0 1 2 3 4 5 6 7 8
11 |
51 |
81 |
91 |
61 |
71 |
31 |
41 |
21 |
100 102 104 106 108 110 112 114 116
n= \({sizeof(arr) \over sizeof(arr[0])}={18 \over 2}=9\)
sort(arr, arr + n, greater<int>());
C++ STL provides a similar function sort that sorts a vector or array (items with random access). It generally takes two parameters, the first one being the point of the array/vector from where the sorting needs to begin and the second parameter being the length up to which we want the array/vector to get sorted. The third parameter is optional and can be used in cases such as if we want to sort the elements lexicographically.
sort() takes a third parameter that is used to specify the order in which elements are to be sorted. We can pass the “greater()” function to sort in descending order. This function does a comparison in a way that puts greater elements before.
And, finally prints the array in descending order.
Hence the correct answer is 91 81 71 61 51 41 31 21 11.