Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. (9 Pts) Suppose we want to read an array size from user, but worry about the user entering 0 or a neg. 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. (9 Pts) Suppose we want to search for int x in array A of size n.
    int i = 0;
    while(A[i] != x && i < n)
      i++;
    if (i == n)
      cout << "Not found!";
    else
      cout << "Found!";
    cout << endl;
    
    int i = 0;
    while(i < n && A[i] != x)
      i++;
    if (i == n)
      cout << "Not found!";
    else
      cout << "Found!";
    cout << endl;
    
    for(int i = 0; i < n && A[i] != x; i++)
      ;
    if (i == n)
      cout << "Not found!";
    else
      cout << "Found!";
    cout << endl;
    
    works / doesn't work
    If doesn't work, why?
    works / doesn't work
    If doesn't work, why?
    works / doesn't work
    If doesn't work, why?
  3. (82Pts) Write a program that will read in a file of points in a format like this:
    N = 6
    X:	1	1.5	2	3.1	6	8.3
    Y:	5.5	5.2	5.1	5.2	5.7	6.6
    
    and output them as ordered pairs, i.e. like this:
    (1,5.5) (1.5,5.2) (2,5.1) (3.1,5.2) (6,5.7) (8.3,6.6)
    
    Turn In a screen capture of your program running on the file Intput.txt and the source code for your program. (Hint: This is most easily done with two arrays.)