CS12, Program 1

Assigned: April 12th, 2001

Due: April 24th, 2001, 10am

 

Topics

 

·         Using user-defined types

·         Assembling a project with multiple files

·         Proper use of #include statements

·         Review of CS10 topics: functions and parameters, return values, loops, input and output. statements

·         Turning in the project.

 

 

Random walk

 

Random walk is a concept from mathematics and physics that is used to explain how molecules move in an enclosed space. The idea is that at any time step, motion can occur in any direction. The model can be used to predict how far from its point of origin a molecule will end up after a certain time period, what is the farthest point it will reach within the time period, and how many times it will ``visit'' a particular point.

 

In this program, we will examine a simple form of random walk -- in one dimension along a line. Imagine a frog that lives in a long, narrow pond covered by lilly pads lined up in a row. The frog decides which way to hop by flipping a coin. If it comes up heads, the frog hops left, tails it hops right. This process repeats a specific number of steps and then stops. In more precise terms, you can imagine the frog on a number line, starting at zero. Based on the result of the coin flip, the frog will add or subtract one from its current position.

 

Write a program to simulate random walk in one dimension. The walk itself should be in a function int walk(), which will then be called from the main(). Your function should accept the following parameters:

int hops -- the number of times the frog should hop

int & farthest -- a variable containing the farthest point from the origin that the frog has reached

int & orig -- a variable that will keep track of the number of times that the frog lands back on the starting point

bool verbose -- if TRUE, the function will print out each step of the walk. Otherwise, the function will output nothing.

The function should return the final position of the frog (a positive or negative integer).

 

Once the function walk() is written, write the main program, which should prompt the user for the length of time the frog should hop, and the printing preference (all steps or none). The program should then output the final position of the frog and the farthest point it reached from the origin during its random hops.

 

 

To Do

 

Use the files dice.h, dice.cpp, and main.cpp from lab. You will be completely rewriting the main.

Simulate a coin toss with a two sided Dice object. Your program should adhere precisely to the specifications detailed above. You may add more functions if you feel they are necessary, but your program must contain the function walk() as described.

Comment your code liberally. At least 10% of your grade comes from style and comments. Please put your name, and lab section at the top of main.cpp.

 

What to turn in

 

Three files, all inside one folder with a name of your choice:

·         dice.h

·         dice.cpp

·         main.cpp

Please clean up and remove all other files and folders from this folder before you turn it in.

 

Extra credit

 

Add another function walk2D(), with the same parameters as int walk(), that simulates a two dimensional random walk. In other words, the frog is now on a grid, and can jump in four directions. This is not trivial. You will need a structure to hold the coordinates of the frog position (this will also be the return type of the function). You will also need a function that will calculate distance from the origin. You will receive more credit if you implement position properly, as a class. Yes, this does mean turning in two more files.

 

  Please note the word “add” at the top of this paragraph.  This is in addition to the required part of the assignment, and should not replace it.