Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [6pts] Suppose we want to read an array size from user, but worry about the user entering 0 or a negative num.
    
    int n;
    cin >> n;
    double *Z;
    Z = new double[n];
    if (n <= 0) {
      cout << "Error!" << endl;
      Z = new double[10];
    }
    
    Z[0] = 5;
    
    
    int n;
    cin >> n;
    if (n <= 0){
      cout << "Error!" << endl;
      double *Z = new double[10];
    }
    else
      double *Z = new double[n];
    
    Z[0] = 5;
    
    
    int n;
    cin >> n;
    double *Z;
    if (n <= 0){
      cout << "Error!" << endl;
      Z = new double[10];
    }
    else
      Z = new double[n];
    
    Z[0] = 5;
    
    circle one:
    • compile-time error
    • possible run-time error
    • OK
    If error, why?
    circle one:
    • compile-time error
    • possible run-time error
    • OK
    If error, why?
    circle one:
    • compile-time error
    • possible run-time error
    • OK
    If error, why?
  2. [5pts] Consider the following program. Write below the output of the program. Try it without compiling and running the code.
    
    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];
    }
    
    
    void foo1(int x, int y){
      x++; y++; 
    }
    
    void foo2(int* x, int y){
      x[y]++;
    }
    
    output:
  1. [89pts] 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: reverse
    cmd: show
    [0] *************************
    [1] ********************
    [2] ***************
    [3] **********
    [4] *****
    cmd: quit
    
    The program processes various commands:
    • show: The program shows all the numbers. See the sample run for formatting.
    • swap i j: The program swaps the positions of the ith number and jth number.
    • reverse: The program reverses the order of the numbers.
    • 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.
    • For reverse, you may need a temporary array. Don't forget to delete it when you don't need it anymore.
    • It will helpful to write a function to print one line.

    Turn In the codeprint of your source code.

    Submit:

     ~/bin/submit -c=IC210 -p=hw20 hw.cpp