CS W1007x Introduction to Computer Science
Homework #2 (9 points)
Life, the Universe, PacMan, and Mastermind
Section 2: Wednesday, February 25. Section 1: Thursday, February 26.
do not hand it in at classtime, it will be your responsibility
to find your TA before 9pm (which may not be possible).
Chapter 4, the first few pages of Section 9.4 (on strings),
and all of Section 11.1 (on arrays).
Your Mission: Three programming projects; three source code files;
Only hand in the last 3 of the 4 projects listed below.
You're source code for these files should be handed in both
electronically, using "submit" as for HW 1, and also physically,
as a hard copy. This way, your TA can try your code, and also
has a copy to write comments on.
- Diagonalized output -- this is a practice problem which will
not be graded and should not be handed in. Let the user type in a string,
and then print this string out diagonally. For example,
Please enter a string: User types this string
To do this, print each
character on a seperate line, with n - 1 spaces before the
nth character. For example, the first character (character
number 0) will appear at the beginning of a line, but character
number 9 will appear after 9 spaces. Here are a few technical
words of wizzdom to help:
- to print a space simple do
- the string library, included by placing the following at the top of
will give you access to two useful functions,
StringLength(string), and IthChar(string,i). You
can probably make an educated guess how to use them, or you can
look them up in the index of your text for more details.
- To print a character, use %c in printf.
- Perfect numbers.Write a program that allows the user
to type in an integer, and tells the user whether it is perfect.
A perfect number is equal to the sum of all its factors other than
itself (every number is its own factor). The factors are the numbers that divide evenly into it. For example,
6 is perfect because if you add up its factors (1, 2, and 3, but not 6),
you get 6.
10 is not perfect since 1+2+5 does not equal 10.
- PacMan Don't get too excited -- you'll draw him, but
he won't move. However, you do get to make him whatever color you
think he really ought to be, and of course, you are welcome to add a
bow to make it Ms. PacWoman. In any case, the eye is optional, so
this is really just a circle-drawing exercise. There are two parts
to your PacMan assignment:
- Simple circle with sine and cosine: Don't fret, trigonometry is
Write a loop that draws a filled-in circle by
from the center of the circle to all points on the circumference.
The counter variable of this loop, angle, should be degrees, from zero
to 360 degrees (or 2 pi radians). For each value of angle, compute
x- and y-coordinates with equations that use sine and cosine (draw
a picture of a right triangle, and solve for x and y, as shown in class).
Here are a few technical
words of wizzdom to help:
- You will need to include the math library.
Newsflash: for your info, if you were
not using gccx, but were using gcc instead, you would need to use
the -lm option.
- sin(AngleInRadians) and cos(AngleInRadians) will then be
available. They take the angle in radians, not in degrees, so
- Start from the code shown in class:
- PacMan Action: Put a loop around the circle-drawing loop, and
necessary modifications so that at least one row of PacMan images are shown,
alternating between open-mouthed and closed-mouthed (he's eating). This
is not motion, just a bunch of "still shots" all shown at once.
- Mastermind. This is a game you will program the computer to
play, using arrays. In this game, the computer randomly picks 3 numbers,
the range 1
through 4. Think of this as 3 slots, with 4 possible values per slot.
You, in turn, keep guessing what the 3 values are until you get
them correct. Each time you guess, the computer
gives you hints about how close you came by telling you two things:
For example, if the computer picks (4,2,3) and you guess (4,1,2), then
the computer would tell you that you're WRONG, however it
would also respond with "1" and "2", respectively. Note that the 4 you
guessed counted in both parts of the hint.
- How many slots you got the correct value for.
- How many of the values you guessed appear somewhere in the computer's
choice. That is, how many correct values you got, even if in the wrong slot.
Each of your guesses can only count once in the second part of the
hint. For example, if the computer picks (2,2,2) and you guess (1,2,3),
the hint is "1" and "1". However, if the computer picked (1,2,3)
and you guess (2,2,2), the response should be "1", "3".
You will need to include the random library with,
in order to generate random values. RandomInteger(low,high)
will then produce a random integer between low and high,
and will give you a different one each time you call it. However,
it will always produce the same sequence of values unless you call
Here are requirements and suggestions for your implementation:
- It ends when the user guesses correctly, and it tells the user how
many guesses it took (the less guesses the user took, the more likely it
is that the user is either really smart, or cheated somehow).
- It must be easy to change the game to any number of slots and any
number of possible values per slot, by simply changing two constants
defined at the top of your source code file. To do this, you'll have
to use an array to hold the computer's choice of values, and another
array to hold the user's guess. Also, you'll need to use well-planned loops
to compare the guess to the computer's choice and produce the two
values that compose a "hint".
- To help you test and debug your code, put printf statements
that show what the computer's choice is, so you know if the hints it
produces are correct. If they are not, put more printf statements
inside the loops that calculate the hint values. These are for
debugging purposes only, so remove these statements (and try your
program again) before handing it in.
- Code for all assignments should be well organized and well commented.
- Extra credit: After you get it working, incorporate
graphics in some way, using colors instead of numbers for slot
values. The graphics window can show the user's guesses, and finally
the correct answer.
Each of the three programs to be submitted will be graded
out of three points:
- One point: does the program work and fulfill the requirement of
- One point: elegance and efficiency of your solution.
- One point: formatting, commenting, and decomposition. (You are not
required to use
functions for this assignment. If you would like to use functions,
however, please feel free to do so.)
email: evs at cs dot columbia dot edu
(Thanks to Andrew Kosoresow for helping develop this handout.)