Lecture 5
Today: Hands-on lab, array applications and preparation for Hw3
Reading: Game of Life handout.
Web
page with the Game of Life, including program in C.
The goal of this lab is:
- to get you up to speed with two-dimensional
array manipulation and structured program development using functions,
in preparation for Hw3.
- to see how much you can do in 2 hours and a half, working under time
pressure.
- to stimulate you to get organized, and apply the software
engineering principles you learned (use of function stubs, test
driver,
keeping versions, debugging) so that by the
deadline (10:20) you have submitted a program that compiles and
implements a minimal functionality, together with a typescript.
- to review arrays, for loops, switch statements, in the context of C++.
To do
Write C++ functions for (as many as you can of) the following tasks:
- Read a game of Life configuration from user
input. The input should be presented as a sequence of characters:
dots '.' for
empty cells and x's for occupied cells.
You should enter one
row per line, ending in a * character (which will not be entered
in the internal array, but rather acts as a delimiter to simplify
the reading function).
The number of columns is the number of characters in the first
row (if the user makes a mistake later, you should check that).
The number of rows of your Life game is the
number of lines you enter. Finish with a # character on the last line.
- Print a Life configuration on the standard output. This has
to be done in a nicely readable manner, each row on a separate line.
- Count the number of cells which are alive in a given Life configuration.
- Copy a Life configuration from an array a into
another array b.
- Given two Life configurations a and b,
compare to see if they are the same.
To test these functions, you will write a main program which asks the
user what to do, then depending on the choice, will execute one of the
preceeding functions or quit. You should allocate two arrays in
the main program, and offer the user the option to enter data in
configuration 1 or configuration 2, print one of them, copy and
compare them.
The options should be:
- 1 or 2
to enter a Life configuration (first or second array).
- 3 or 4 to print one of the two internally stored
Life configurations.
- 5 or 6 to count the number of living cells in one of
the two Life configurations.
- 7 to copy configuration 1 into configuration 2.
- 8 to compare the two configurations.
Hint: start by writing the main program and provide empty functions
for the 5 desired ones. Then implement each function, one by one, in
the logical order which will allow you to test them.
Submitting
You should develop your program in stages, keep intermediate versions and
submit the latest working version in Lab5,
before leaving the lab at 10:20. Name the file lab5.cpp and
submit a typescript, too.
You should moderately comment your program,
write on it the names of both lab partners, if
you didn't work alone, and relax: the lab will not be harshly graded. I want
to see what each of you (or teams) can do when working under time pressure.
After the lab: you can continue working and thinking about ways of
completing this lab, but do not submit
anything. Use the work done in the lab to complete Homework 3.
Overview of
homework 3: the Game of Life.