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. [20pts] Consider the following code snippet.
    
    struct Grades
    {
      double* A;
      int N;
      string name;
    };
    
    int main()
    {
      int n;
      Grades* A = readfrom("data.txt", n);
      cout << "There are " << n << " entries" << endl;
      sort(A, n);
      int k = search(A[0].A, 20, 75.5);
      cout << A[0].A[k];
      return 0;
    }
    
    1. [14pts] For each expression on the right, if it were used inside the main function, what would be the type of the expression? Write the type, or write "error" if it will cause a syntax error.
    expressiontype
    A
    A.name
    A.name[0]
    A.A
    A.A[0]
    A.N
    A.N[0]
    A[0]
    A[0].name
    A[0].name[0]
    A[0].A
    A[0].A[0]
    A[0].N
    A[0].N[0]
    1. [6pts] For each function used in the above code, write the prototype that would match the function.
      // write below the prototype of readfrom
      
      
      // write below the prototype of sort
      
      
      // write below the prototype of search
      
      
      
  3. [70pts] Write a program hw.cpp that reads in a file like cardata.txt and stores for each car the "tag", which is a character identifying the car, and the location (in miles relative to our current location) and velocity (in mph) of the car. The program will then print the locations of the cars initially, and after 1, 2, ..., 8 hours. So for the example file cardata.txt, the run would look like this:

    ~/$ ./hw
    cardata.txt
    hour 0 [A:-120][X:20.5][C:7][M:-5.5]
    hour 1 [A:-97.5][X:6.5][C:27][M:-14]
    hour 2 [A:-75][X:-7.5][C:47][M:-22.5]
    hour 3 [A:-52.5][X:-21.5][C:67][M:-31]
    hour 4 [A:-30][X:-35.5][C:87][M:-39.5]
    hour 5 [A:-7.5][X:-49.5][C:107][M:-48]
    hour 6 [A:15][X:-63.5][C:127][M:-56.5]
    hour 7 [A:37.5][X:-77.5][C:147][M:-65]
    hour 8 [A:60][X:-91.5][C:167][M:-73.5]

    Obviously, given today’s topic, I'm expecting you to use structs for this!

Turn in