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] Provide the type of each expresssion. Write ERROR if the expression is illegal.
    
    int x = 1;
    int* px = &x;
    char c = 'a';
    char* pc = &c;
    string s = "hello";
    
    expression type expression type
    &x *x
    px &px
    *px *s
    pc *pc
    *c &c
  3. [10pts] Consider the following program:
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
      int a = 10, b = 7;
    
      // the first cout-statement
      cout << "&a=" << &a << ", &b=" << &b << endl;  
    
      int* p;
      uint64_t addr;          
      cin >> hex >> addr;     
      p = (int*) addr;        
    
      int n;
      cin >> n;
      *p = n;
    
      // the second cout-statement
      cout << "a=" << a << ", b=" << b << endl; 
      return 0;
    }
    
    Suppose the program output was as follows:
    &a=0x7ffff83497e0, &b=0x7ffff83497e4
    a=10, b=20
    
    1. What was the input from the user?
    2. Give a simple diagram (i.e., with three boxes each for a, b, p) in this situation, when the program control is at the end of main.

  4. [10pts] What will be the output of the following program? Expect a similar question in the exam. See if you can do this manually.
    
    #include <iostream>
    using namespace std; 
    
    int foo(double x, double* y);
    
    int main() 
    {
      double a = 8.5;
      double b = 10.1;
      cout << "ONE " << foo(a, &b) << endl;
      cout << "TWO " << a << " " << b << endl;
      return 0;
    }
    
    int foo(double x, double* y) 
    {
      x = x + 2;
      *y = *y + 2;
      cout << "THREE " << x << " " << (*y) << endl;  
      return x;
    }
    
    Give the output:
    
    
  5. [60pts] Download hw.cpp. The program contains a main which reads in two candidate names, then reads in a bunch of votes, and finally says who won or if there was a tie. You must not change main(), but should complete the prototypes and definitions of the functions needed for the program to work. In particular, you will need to define (at least) the three functions get_names, update_counts, and display_results.

    Here is a brief description of each function. Of course, the best way to understand how each function needs to work is by looking at the code in hw.cpp that uses these functions, and the sample runs below.

    ~$ ./hw
    Candidate names: Pepsi Coke
    Enter votes, ending with "END":
    Pepsi
    Pepsi
    Coke
    Coke
    Coke
    Pepsi
    Coke
    END
    Coke wins with 4 of 7 votes
    ~$ ./hw
    Candidate names: IT CS
    Enter votes, ending with "END":
    IT
    CS
    CS
    IT
    Aero
    Invalid name
    IT
    IT
    CS
    CS
    EE
    Invalid name
    END
    Tie!
    
    ~$ ./hw
    Candidate names: Tacos Sushi
    Enter votes, ending with "END":
    Tacos
    Pizza
    Invalid name
    Sushi
    Tacos
    END
    Tacos wins with 2 of 3 votes
    

Turn in