SI 204 Spring 2017 / HWs


Homework 22: Functions with arrays

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Friday, March 10
  • This homework contains code to be submitted electronically. Put your code in a folder called hw22 and submit using the 204sub command.
  • This is a written homework - be sure to turn in a hard-copy of your completed assignment before the deadline. Use the codeprint command to print out your code and turn that in as well.

Assignment

  1. Circle one to indicate how you did for the reading assignment from Homework 20 before class on Monday:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  2. Circle one to indicate how you did for the reading assignment from Homework 21 before class on Wednesday:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  3. Given the following definitions:
    double y = 2.25;
    int n = 2;
    char s[10] = "hard";
    int A[3] = {6, 9, 8};
    double* X = calloc(3, sizeof(double));
    X[0] = 5.1; X[1] = 5.4; X[2] = 5.7;
    
    double sqr(double z) { return z*z; }
    
    int derp(int* kptr) {
      if (*kptr < 5) {
        return 1;
      } else {
        return 99;
      }
    }
    fill in the following table with the type and value of each expression. Write ERROR for both if the expression would be a compiler or run-time error.
    expressiontype (or ERROR) value (or ERROR)
    X[0]
    A[1] - y
    A[0] - X[n]
    ++A[2]
    A[1] + s[1]
    sqr(A[0])
    sqr(X[0])
    derp(&n)
    derp(A)
  4. Write a program called substring.c that reads in a target string and a test string, and reports "Yes" or "No" depending on whether the target is a substring of the test string.

    T is a substring of S means that T appears (in order and together!) in S somewhere. For example: "low" is a substring of "allowable" (see it? "allowable"), but not of "withhold" (see the letters are there but not in order in "withhold"), nor in "landowner" (see the letters are there and in order but not together in "landowner").

    Example runs:

    roche@ubuntu$ ./substring
    Target: isp
    Test: mississippi
    no
    roche@ubuntu$ ./substring
    Target: sip
    Test: mississippi
    yes
    roche@ubuntu$ ./substring
    Target: issi
    Test: mississippi
    yes
    roche@ubuntu$ ./substring
    Target: sss
    Test: mississippi
    no
  5. Write a program called pairs that reads and integer N, and then 2 rows of N points each, and then prints out the points in (x,y) pairs, like this:
    roche@ubuntu$ ./pairs
    N: 6
    x values: 1    1.5  2    3.1  6    8.3
    y values: 5.5  5.2  5.1  5.2  5.7  6.6
    (1,5.5) (1.5,5.2) (2,5.1) (3.1,5.2) (6,5.7) (8.3,6.6)
    roche@ubuntu$ ./pairs
    N: 3
    x values: 1 2 3
    y values: 4 5 6
    (1,4) (2,5) (3,6)