CS W1007x Introduction to Computer Science

Homework #2 (9 points)

Life, the Universe, PacMan, and Mastermind

Due Date

Section 2: Wednesday, February 25. Section 1: Thursday, February 26. If you 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, printed out as a hard copy. This way, your TA can try your code, and also has a copy to write comments on.

  1. 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:
  2. 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.
  3. 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:
    1. Simple circle with sine and cosine: Don't fret, trigonometry is your friend! Write a loop that draws a filled-in circle by plotting lines 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.
        #include <math.h>
      • sin(AngleInRadians) and cos(AngleInRadians) will then be available. They take the angle in radians, not in degrees, so watchout!.
      • Start from the code shown in class: trygraphics2.c code
    2. PacMan Action: Put a loop around the circle-drawing loop, and make any 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.
  4. Mastermind. This is a game you will program the computer to play, using arrays. In this game, the computer randomly picks 3 numbers, each in 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:
    1. How many slots you got the correct value for.
    2. 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.
    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.

    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,

    #include "random.h"
    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 Randomize().

    Here are requirements and suggestions for your implementation:


Each of the three programs to be submitted will be graded out of three points:
email: evs at cs dot columbia dot edu (Thanks to Andrew Kosoresow for helping develop this handout.)