Write a function in (C++) named - accending_order - that takes a
reference to a vector of string (like {"HI 32", "HELLO 78", "NAME
97", "WORLD 07"}), and returns nothing. The function should reorder
the vector so that the courses are sorted by number in ascending
order.
Write this function without - int (main).
CODE IN C++ ONLY.
EXPECTED OUTPUT: {"WORLD 07", "HI 32", "HELLO 78", "NAME 97"}
Solution :
#include <bits/stdc++.h>
using namespace std;
void ascending_order(vector<string> &vec) {
for(int i=0;i<vec.size();i++) {
for(int j=i+1;j<vec.size();j++) {
int n1 = stoi(vec[i].substr(vec[i].find(' ')+1));
int n2 = stoi(vec[j].substr(vec[j].find(' ')+1));
if(n1 > n2)
swap(vec[i],vec[j]);
}
}
}
Output reason:
It is mentioned in the question that programmer do not have to write the main function, so the function ascending_order is not called anywhere hence there is no output.
Get Answers For Free
Most questions answered within 1 hours.