Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
926 views
in Information Technology by (114k points)
closed by

What will be the output of the following?

#include <bits/stdc++.h>

using namespace std;

int main()

{

int arr[] = {11, 51, 81, 91, 61, 71, 31, 41, 21};

int n = sizeof(arr) / sizeof(arr[0]);

sort(arr, arr + n, greater<int>());

cout << "Output Array : \n";

for (int i = 0; i < n; ++i)

cout << arr[i] << " ";

return 0;

}

1 Answer

0 votes
by (113k points)
selected by
 
Best answer
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.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...