CS 1005, C Term 2000
Introduction to Programming
HW2 (due Feb. 4)

This page is located at http://www.cs.wpi.edu/~alvarez/CS1005/HW2/


In this assignment you will design a program that simulates a random walk along a street. Your program will be a client program of the Dice class described in section 5.4 of Astrachan and will require the use of conditional control and iteration constructs. This will be the first assignment in which you will need to use several source files together for the same program.

Random walks

The basic idea is that in order to walk randomly along a street, you toss a coin before each step. If the coin comes up heads, you step to the right; if it comes up tails, you step to the left. A coin is really the same as a "two-sided die", so the Dice class can be used to simulate a coin. Your program should prompt the user for the total number of steps to be taken and should then iterate the process of coin tossing followed by stepping, reporting its progress as it goes along. Detailed instructions appear below.


  1. Review the material from recent lectures and from chapters 4 and 5 of Astrachan, paying particular attention to section 5.4, in which the Dice class is described. Remember that program examples are also available in the appropriate subdirectories of the directory named /cs/cs1005/ on the CCC Unix network.

  2. Log into your CCC Unix account. Inside your personal CS1005 directory, create a subdirectory named HW2. Keep all files for this assignment in that directory. Files containing the specification and implementation of the Dice class are available in the /cs/cs1005/samples/hw2/ directory. Copy these files to your personal HW2 directory.

  3. Edit a (new) text file named hw2.cxx using your favorite text editor. Your completed program will use the Dice class, so you will need to add the appropriate #include directive in the file hw2.cxx to include the Dice class header file. Do this now. See section 5.4 for examples of programs that use externally defined classes. Start off your main() function in hw2.cxx by just defining a 2-sided Dice object named coin. Add a return statement, and compile this minimal version of your program using the g++ compiler. Since you are including the Dice class, you will need to specify some additional files when compiling/linking. Use the following command line:
       g++ -o walk hw2.cxx dice.cpp randgen.cpp
    If the compiler reports errors or warnings at this stage, fix the problems before proceeding further.

  4. Having taken a first look at the technical issues related to including the externally defined Dice class and compiling and linking the various files involved, you can proceed to design and implement your program. As described above in the introduction, the program should prompt the user for the number of steps to be taken, and it should report the evolution of a random walk with that many steps as it is carried out. The user should be able to select one of two output modes: text-only, and (crude) graphics. Two sample runs illustrating the desired behavior of the program may be found here and here. Further embellishments to the program's interface are optional. Use functions to encapsulate different tasks within your program. In particular, input and output should be performed by appropriate functions. The main() function doesn't need to contain much more than the necessary function calls, except for object declarations and initializations, the main loop for updating the walker's position based on coin tosses, and one or two appropriate if-else statements.

  5. Compile your program as described above and test it for different combinations of walk length and output mode. Debug it as necessary. It's often a good idea to implement one function at a time, proceeding to the next function only after the previous one is behaving correctly. Once you're satisfied that your program is behaving as desired, generate a script file named hw2.script that shows several runs of your program. To do this, just type script hw2.script at the Unix prompt. Then run your program as usual. When you're done, type exit to discontinue the logging process.

  6. The source code for your program in hw2.cxx should be carefully commented. Provide additional documentation for your code in a file named hw2.readme, following the WPI CS documentation standard


Your program will be graded based on syntactical soundness, functionality, style, and documentation, with points awarded as indicated below. Note that it's possible to get up to 20 bonus points for this assignment.


Use turnin to submit the following files (and no other files), fully addressing the items listed above, before 3 pm on Friday Feb. 4, 2000.
  1. hw2.cxx
  2. hw2.readme
  3. hw2.script