Name: ____________________________________________________ Alpha: _________ Section: ____________

Describe help received: _________________________________________________________________

See the bottom for how to submit your work.

Due: Before 0800 on next class day

  1. (10 pts) Read today's lecture notes. Honestly declare how you read the notes.
    1. (10 pts) I read the notes carefully. I have a good understanding of the topics therein.
    2. (10 pts) I read the notes carefully, but I don't really understand the following points well:
      
      
      
    3. (0 pts) I didn't read the notes carefully.
  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 memory diagram when the program control is at control_point_1. The diagram should look similar to what's in the notes except that you can omit the code. That is, you need to describe:
      • The stack frame for each active function.
      • Variables in each stack frame along with their values (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
    Numbers: 10 5 20 15 25
    cmd: show
    [0] **********
    [1] *****
    [2] ********************
    [3] ***************
    [4] *************************
    cmd: swap 0 1
    [0] *****
    [1] **********
    [2] ********************
    [3] ***************
    [4] *************************
    cmd: swap 2 3
    [0] *****
    [1] **********
    [2] ***************
    [3] ********************
    [4] *************************
    cmd: quit
    

    The program reads a size N and N integers from the user and processes the following commands:
    • show: The program shows the data as in the sample run (displays as many stars as the integer value at each position read from the user).
    • swap i j: The program swaps the positions of the ith number and jth number. After processing the command, the program should show the data as in the sample run.
    • 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.

Submit to the submission server (due: 0800 on the next class day)

~/bin/submit -c=IC210 -p=hw22 hw.cpp
Bring to class