Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [12pts] 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;
    
    compile-time error / possible run-time error / OK
    If error, why?
    compile-time error / possible run-time error / OK
    If error, why?
    compile-time error / possible run-time error / OK
    If error, why?
  2. [88pts] Write a program that reads n points from standard input in the following format
    N = n
    (x1,y1) (x2,y2) ... (xn,yn)
    
    and writes out the x-coordinates of all n points on one line, and the y-coordinates of all n points on the following line. The output must be formatted like this:
    X := [x1, x2, ..., xn]
    Y := [y1, y2, ..., yn]
    
    Here are some example runs. Your program must read input and produce output with the exact same formatting!
    ~/$ ./hw
    N = 4
    (0.5,1) (0.6,1.5) (0.7,2) (0.8,2.5)
    X := [0.5, 0.6, 0.7, 0.8]
    Y := [1, 1.5, 2, 2.5]
    ~/$./hw
    N = 1
    (5,8)
    X := [5]
    Y := [8]
    ~/$./hw
    N = 10
    (11,51) (12,52) (13,53) (14,54) (15,55) (16,56) (17,57) (18,58) (19,59) (20,60)
    X := [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    Y := [51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
    
    Note: You may assume there is at least one point, i.e. you never get input like N = 0.

    Turn In your source code and a screen capture of your program running on above example. (Hint: This is most easily done with two arrays.)