SI 204 Spring 2017 / HWs

This is the archived website of SI 204 from the Spring 2017 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 38: C++ practice

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Friday, April 28
  • This homework contains code to be submitted electronically. Put your code in a folder called hw38 and submit using the 204sub command.
  • This is a written homework - be sure to turn in a hard-copy of your completed assignment before the deadline. Use the codeprint command to print out your code and turn that in as well.


  1. Circle one to indicate how you did for the reading assignment from Homework 36 before class on Monday:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  2. Tell me about two separate capstone or research projects you attended on Wednesday (see HW37 for details).
    • (1) List the name of the capstone, (2) summarize what the project is about in two or three sentences of your own, and (3) provide some judgment (what did you like, what seemed most challenging, what impressed you, etc.).
    • (1) List the name of the capstone, (2) summarize what the project is about in two or three sentences of your own, and (3) provide some judgment (what did you like, what seemed most challenging, what impressed you, etc.).
  3. Given the following C++ declarations:
    ifstream fin("myfile.txt");
    string a;
    vector<string> v1;
    vector< vector<int> > v2;
    fill in the following table with the type (only) of each expression. Write ERROR for if the expression would be a compiler or run-time error. Use the types istream and ostream for input and output streams (respectively).
    expressiontype (or ERROR)
    1 == 2
    a = "hello"
    fin >> a
    cout << 13 << endl
  4. Below is a partial program hiscore.cpp that reads in names and scores and reports who had the hightest score. The struct definition for result and the main function are already written for you and you may not change the struct definition or the main.
    struct result {
      string name;
      int score;
    int main() {
      int n = 5;
      cout << "Enter " << n << " results:" << endl;
      vector<result> res;
      for (int i=0; i < n; ++i) {
        result temp;
        cin >> temp;
      // find highest score
      result best = res[0];
      for (int i=1; i < res.size(); ++i) {
        if (best < res[i]) {
          best = res[i];
      // print highest score
      cout << "The best result is " << best << endl;
      return 0;

    Your job is to add any overloaded operator definitions and #includes so that the program works as shown in the following example:

    roche@ubuntu$ ./hiscore
    Enter 5 results:
    Bob 100
    Mary 150
    Susan 80
    Jane 200
    Robby 150
    The best result is Jane (200 points)
  5. Write a program sched.cpp that reads in the file hwin.txt that contains course section data in typical USNA format, then goes into a loop allowing the user to enter queries like "meets M2" that it responds to by printing out all the sections that meet during that day/period.

    The period entered is one of the regular periods 1,2,3,4,5,6, but half-period overlaps should count. I.e. if the query is "meets T2", and section that meets TR8 should be printed out, even if technically there is only half a period of overlap.

    A run of the program might look like this:

    roche@ubuntu$ ./sched
    file: hwin.txt
    command: meets W4
    HE112 4001 MWF4
    HE112 9001 WF9
    SC112 4000 TRF4,W34
    SC112 4040 M4,WF4,T56
    SC112 5534 MTR5,W34
    command: meets F6
    command: meets T1
    EE301 1111 M1,TR12
    SC112 8812 TR8,W12
    SC112 8012 TR8,F12
    command: quit

    Of course your program should also handle other input files (that are similarly formatted), and should use all of the great C++ features such as <string>.

    To make your like easier, I am gifting you the following function (read the documentation about what the function does for you very carefully but don't bother trying to understand how the function works!):

     Input: pat - a string representing a meeting time,
                  e.g. "MWF2,R34" or "TR9" or "MF5,T65"
            day - a char, one of M,T,W,R,F
            per - an int, one of the regular periods, i.e. 1,2,3,4,5,6
     Output:true if the meeting time in pattern pat overlaps with period day,per,
            false otherwise
            Ex1 - overlaps("MWF2,R12",'M',8) -> true
            Ex2 - overlaps("TR10",'T',4) -> false
    bool overlaps(string pat, char day, int per) {
      bool dflag = false; // day match flag
      for(int i = 0; i < pat.length(); ++i) {
        if (pat[i] == ',') {
          dflag = false;
        } else if ('A' <= pat[i] && pat[i] <= 'Z') {
          dflag = dflag || pat[i] == day;
        } else {
          int q;
          if (pat[i] == '1' && i+1 < pat.length() && pat[i+1] == '0') {
            q = 10;
          } else {
            q = (pat[i] - '0');
          if (dflag && (per == q || (per-1)/2 + 8 == q)) {
            return true;
      return false;