Question

#C++ Write a function, named sortByCourseNumber, that takes a reference to a vector of string (like...

#C++

Write a function, named sortByCourseNumber, that takes a reference to a vector of string (like {"CSE 232", "ECE 100", "CSE 450", "CSE 232"}) and returns nothing. The function should reorder the vector so that the courses are sorted by number in ascending order. You can assume that the department code is separated from the number by a space and that all course numbers are 3 characters long.

You should be using the STL algorithms to achieve this, full credit is only possible if your solution doesn't have any looping constructs (no "while" or "for" keywords anywhere in the solution). If a URL you are citing has such a word, add an interupting space like: https://www.programiz.com/cpp-programming/fo r-loop

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have used for loop just for displaying the vector. It has nothing to do with sorting the vector.

Here is the code for your problem.

#include <bits/stdc++.h>

using namespace std;

// compare function for sorting

bool compare(string s1, string s2)

{

    // these 2 variables store last 3 digits of the string

    // it is mentioned in the question that the length of code is 3

    stringstream num1(s1.substr(s1.length() - 3));

    stringstream num2(s2.substr(s2.length() - 3));

    // converting string to integer

    int code1, code2;

    num1 >> code1;

    num2 >> code2;

    // returning true if code1 is less than or equal to code2

    return (code1 <= code2);

}

// main function

int main()

{

    // initializing vector

    // this is just for testing purpose

    vector<string> course = {"CSE 232", "ECE 100", "CSE 450", "CSE 232"};

    // this is the sort method call using compare function

    sort(course.begin(), course.end(), compare);

    // I have used for loop just for displaying

    // it has nothing to do with sorting

    for (string i : course)

    {

        cout << i << endl;

    }

    return 0;

}

Here is the screenshot of the code if the indentation is not clear.

Here is the output of the code.


Hope this helps.

Please rate the answer if you like it.

Do leave a comment.Any suggestion/query is much appreciated.

Add a comment
Know the answer?
Add Answer to:
#C++ Write a function, named sortByCourseNumber, that takes a reference to a vector of string (like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a function named "FriendsWithPets" that takes a const reference to a std::map of my...

    C++ Write a function named "FriendsWithPets" that takes a const reference to a std::map of my friends names (std::string) to the number of pets they own (int) and returns the number of friends (int) that have at least one pet. You should be using the STL algorithms to achieve this, credit is only given if your solution doesn't have any looping constructs (no "while" or "for" keywords anywhere in the solution).

  • Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make...

    Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics. This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the...

  • In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std;...

    In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std; const int MAXRESULTS = 20; // Max matches that can be found const int MAXDICTWORDS = 30000; // Max words that can be read in int main() { string results[MAXRESULTS]; string dict[MAXDICTWORDS]; ifstream dictfile; // file containing the list of words int nwords; // number of words read from dictionary string word; dictfile.open("words.txt"); if (!dictfile) { cout << "File not found!" << endl; return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT