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. [15pts] Consider the following program.
    
    void foo1(int x, int y);
    void foo2(int* x, int y);
    
    int main() {
      int* A = new int[3];
      A[0] = 0; A[1] = 1; A[2] = 2;
      foo1(A[0], A[1]);
      cout << A[0] << " " << A[1] << endl;
      foo2(A, A[0]);
      cout << A[0] << " " << A[1] << " " << A[2] << endl;
    }
    
    void foo1(int x, int y){
      x++; y++; 
    }
    
    void foo2(int* x, int y){
      x[y]++;
      // control_point_1
    }
    
    1. [5pts] Give below the output of the program. Try it without compiling and running the code. Similar problems will appear in the exam.
      
      
      
    1. [10pts] Draw a diagram when the program control is at control_point_1. The diagram should look simlar to what's in the notes except that you can omit the code. Check the guide in the resources tab of the course homepage. In particular, you need to describe:
      • The stack frames for main() and foo2(). That is, variable(s) in each stack frame along with its value (or an arrow if the variable is a pointer).
      • Array(s)
  3. [75pts] Write a program hw.cpp (download hw.cpp as the starter code) that works as follows:
    ~/$ ./hw
    N = 5
    10 5 20 15 25
    cmd: show
    [0] **********
    [1] *****
    [2] ********************
    [3] ***************
    [4] *************************
    cmd: swap 0 1
    cmd: show
    [0] *****
    [1] **********
    [2] ********************
    [3] ***************
    [4] *************************
    cmd: swap 2 3
    cmd: show
    [0] *****
    [1] **********
    [2] ***************
    [3] ********************
    [4] *************************
    cmd: quit
    

    The program processes the following commands:
    • show: The program shows the data as in the sample run.
    • swap i j: The program swaps the positions of the ith number and jth number.
    • quit: The program ends.

    Requirement.

    • Don't change the main function of the starter code.
    • Add the functions (i.e., prototypes and definitions) outside of the main function.
    • For swap, you may need a temporary integer. If it doesn't work, please use the debugger and check how the variables change.

Turn in