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 the204sub
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
-
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 allSkimmed itRead someRead all -
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 allSkimmed itRead someRead all - Given the following declarations:
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.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;
expression type (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
-
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:- Change the G to a Q
- Change the 18 to a 19
- Swap location (35,4) with location (39,24).
-
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.
When Pierce tries tostrip
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
paint
s, that means adding to the front of the linked list. When Pierce wants tostrip
, 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