# (SOLVED) Sort a vector Define a function named SortVector that takes a vector of integers as a parameter.

Discipline: IT, Web

Type of Paper: Question-Answer

Academic Level: Undergrad. (yrs 3-4)

Paper Format: APA

Pages: 1 Words: 275

Question

Sort a vector Define a function named SortVector that takes a vector of integers as a parameter. Function SortVector() modifies the vector parameter by sorting the elements in descending order (highest to lowest). Then write a main program that reads a list of integers from input, stores the integers in a vector, calls SortVector(), and outputs the sorted vector. The first input integer indicates how many numbers are in the list. Ex: If the input is: 5 10 4 39 12 2 the output is: 39,12,10,4,2, It must be done in C++

Expert Answer

The complete program source code, including the required functions, is given below:

#include
#include
#include

using namespace std;

//functon to display vector
void display(vector<int> vec)
{
for (auto i = vec.begin(); i != vec.end(); ++i)
{
cout << *i << ",";
}
}

void SortVector(vector<int> &vec)
{
//sort the vector
sort(vec.begin(), vec.end(), greater());
}

//main function
int main()
{
//variable declaration
int length, num;
vector<int> v;
//display message
cin>>length;
//fill the vector with random value
for (int i = 1; i <= length; i++)
{
cin>>num;
v.push_back(num);
}

//call the method
SortVector(v);
//display the sorted vector
display(v);
return 0;
}

Explanation:
This program accepts a list of numbers and then displays the numbers in sorted order.

Step 2/2
Final answer
The output of the above program after execution is given below:

This output is as expected.