SI 204 Spring 2017 / HWs


Homework 32: Complex structs and linked lists

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Friday, April 14
  • This homework contains code to be submitted electronically. Put your code in a folder called hw32 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 30 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 31 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 declarations:
    struct point {
      double x;
      double y;
    };
    
    struct trial {
      double stime;
      double etime;
      struct point* ways;
      int numways;
    };
    
    struct subject {
      char name[128];
      int id;
      struct trial pre;
      struct trial post;
    };
    
    struct subject S;
    struct subject* A;
    int i;
    int j;
    fill in the following table with the type (only) of each expression. Write ERROR for if the expression would be a compiler or run-time error.
    expressiontype (or ERROR)
    S.id
    A[i]
    A.pre.ways[i][j]
    S.post = A[i].pre
    A[i].post.ways
    S.pre.ways[i]
    A[i].id = S.ways[i].y
    A[i].pre.ways[j].x
    A[i].post.ways.x
    ++S.pre.numways
  4. Suppose we have the structs defined below, and variable V assigned and initialized as in the picture.
    struct pos {
      int row;
      int col;
    };
    struct guy {
      char sym;
      struct pos loc;
    };
    struct group {
      struct guy* peeps;
      int num;
    };


    Write statements that do each of the following:
    1. Change the G to a Q
    2. Change the 18 to a 19
    3. Swap location (35,4) with location (39,24).
  5. Pierce the Painter likes to paint many layers of colors on top of each other, then strip them off and remember what was underneath. Write a program to help Pierce keep track of these colors.
    Your program will read a series of two kinds of commands:
    • paint color
      Adds a layer of color on top of the current color.
    • strip
      Removes the topmost layer of color, revealing whatever was just underneath.
    Before each command, your program should report what color is on the wall by saying "The top color is X." When there are no colors, like at the beginning of the program, say "The canvas is blank."
    When Pierce tries to strip from a blank canvas, quit the program.

    Of course, you will want to create a linked list to help you! Each node in your linked list should store a single string, for the name of a color. When Pierce paints, that means adding to the front of the linked list. When Pierce wants to strip, that means removing the first node in the list.

    Examples

    roche@ubuntu$ ./paint
    roche@ubuntu$ ./paint
    The canvas is blank.
    strip
    roche@ubuntu$ ./paint
    The canvas is blank.
    paint red
    The top color is red.
    strip
    The canvas is blank.
    strip
    roche@ubuntu$ ./paint
    The canvas is blank.
    paint blue
    The top color is blue.
    paint blue
    The top color is blue.
    paint green
    The top color is green.
    strip
    The top color is blue.
    paint orange
    The top color is orange.
    strip
    The top color is blue.
    strip
    The top color is blue.
    paint purple
    The top color is purple.
    paint purple
    The top color is purple.
    strip
    The top color is purple.
    strip
    The top color is blue.
    strip
    The canvas is blank.
    paint white
    The top color is white.
    strip
    The canvas is blank.
    strip