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] 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?
  1. [80pts] Write a program with source file named hw.cpp that works like this:

    ~/$ ./hw
    N = 3 
    X = [1.1, 2.2, 3.3]
    Y = [1, -1, 0.1]
    <X,Y> = 1.1 * 1 + 2.2 * (-1) + 3.3 * 0.1 = -0.77
    
    ~/$ ./hw
    N = 1
    A = [-1.2]
    B = [-4.3]
    <A,B> = (-1.2) * (-4.3) = 5.16
    

Turn in