Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [10 pts] We would like you to self-test how much you retained from the lecture. Write the correct solution to the Mandatory Practice Problems. Debug your code until it runs correctly. Circle below how you wrote your code.
    1. (10 pts) I was able to write the code without referring to anything.
    2. (10 pts) I had to look at the notes, but still I was able to write the code without looking at the solution.
    3. (10 pts) I had to look at solutions to finish to code.
    4. (0 pts) I didn't do this.
    If you had to look at the solution, briefly describe what you missed but understand now.
    
    
    
    
  2. [10pts] The program toy.cpp listed below is intended to work as follows:
    • Read an integer N and N doubles from the terminal and then print all the negative doubles in reverse order of how they are entered.
    For example, given the following input from the terminal
    5 
    1.1 -1.3 4 -7.4 -3.6 
    ... the program should output the following:
    Negatives in reverse:
    -3.6
    -7.4 
    -1.3 
    However, the program doesn't work as it is intended to. Fix the bugs by annotating in the code directly. (Hint: There are 6 bugs).

    Note: Try to solve the problem by hand (and then double-check your answer by compiling and running your solution).

    
    // fix the bugs!!
     1 #include <iostream>
     2 using namespace std;
     3
     4 void print_negatives(double* A, int N);
     5
     6 int main()
     7 {
     8   int N=0;
     9   double* A = new double[N];
    10   cin >> N;
    11
    12   for(int i=0; i<N; i++)
    13     cin >> A[N];
    14
    15   cout << "Negatives in reverse:" << endl;
    16
    17   void print_negatives(double* A, int N);
    18
    19   delete [] A;
    20
    21   return 0;
    22 }
    23
    24 void print_negatives(int* A, int N)
    25 {
    26   for(int i = N; i >= 0; i--)
    27     cout << A[i] << endl;
    28 }
    
  3. [20pts] Assume the delcarations and function definitions below. fill in the table. Note: "error" is a possiblity! Note: each question is independent, i.e. no side effects of one expression should be taken to carry over to the next.
    
    string* S = new string[2];
    S[0] = "happy";
    S[1] = "sad";
    
    string foo()
    {
      return "good";
    }
    char bar(string str)
    {
      int i = str.length()-1;
      return str[i];
    }
    bool iscap(char c)
    {
      return (c >= 'A' && c <= 'Z'); 
    }
    
    expressiontype (or error!)value (or error!)
    SN/A
    S[1]
    S[1][2] - 'a' + 'A'
    foo() + 2.5
    S[0][0] = S[1][0]
    bar(foo())
    foo()[3]
    bar(S[0][0])
    iscap(S[0])
    iscap(S[0][0])
  4. Write a program in a file named hw.cpp that reads in information about a series of flights and prints out information about the reverse journey.

    After specifying how many legs are in the trip, the user will type in pairs of airports like ABC->XYZ and the time that leg takes, either formatted like 3:02, meaning 3 hours and 2 minutes. Each airport will always be specified by a 3-letter code, separated with ->.

    After reading in information on each leg of the journey, your program should print out the reverse journey in one line with -> between the airport names, and then the total time, formatted like 3 hours 2 minutes. You should be grammatically incorrect and always print hours and minutes even if it's only one hour or one minute.

    If the airports do not match between two consecutive legs (specifically, if the destination airport of one leg is different from the starting airport of the next leg), your program should print out an error message "city mismatch" and exit.

    Example runs:

    ~/$ ./hw
    How many legs? 3
    BWI->JFK 0:40
    JFK->BUF 1:10
    BUF->YYZ 0:30
    Reverse trip: YYZ->BUF->JFK->BWI
    Total time: 2 hours 20 minutes
    
    ~/$ ./hw
    How many legs? 1
    DCA->DFW 2:03
    Reverse trip: DFW->DCA
    Total time: 2 hours 3 minutes
    ~/$ ./hw
    How many legs? 4
    LHR->ORD 9:10
    ORD->OGG 11:12
    OGG->NRT 11:10
    NRT->BNE 9:21
    Reverse trip: BNE->NRT->OGG->ORD->LHR
    Total time: 40 hours 53 minutes
    ~/$ ./hw
    How many legs? 10
    DCA->EWR 0:58
    LGA->YUL 1:26
    city mismatch 

Turn in