CIS 220 - Program 6
Subs, Functions, Parameter Passing
Objectives: Gain experience
modularizing your programs.
Simulations are very important for the development of business applications. They are used to locate bottlenecks in factory operations, used to evaluate pricing policy given consumer reactions, and the College of Business uses simulations as a teaching tool in a number of advanced and graduate courses.
This simulation is designed to provide you with more experience using functions and subs, looping, random numbers, and invoking procedures from other procedures. You will be simulating the life of Hank the Roach, a lab cockroach, who can only skitter along the edges of a cube. At the start of a trial, Hank is randomly placed on one of the eight corners of a cube and a sedative is placed on one of the eight corners. Hank starts moving along the edges. When he comes to a corner, he chooses one of the directions and continues. How many edges will Hank visit until he hits the sedative? Note that Hank may visit zero to an infinite number of edges in a given trial.
Develop a simulation of Hanks travels around the cube. The visual basic form for your Hank simulation should include two buttons, three labels, and two list boxes:
The program uses listboxes because they can hold multiple values, and thus can be used to record or trace Hank's movements during a single trial, or to record the number of edges visited for all the trials.
Use a delay routine to slow Hank's movements so that they can be observed. The interface that you should use is shown in the following figure.
The use of labels in a form, instead of textboxes, will speed up larger programs because they have fewer properties. However, you may need to refresh the label with a command like lblNumEdges.Refresh before the new value will be displayed.
Use two integers declared as static variables, to track the running average. Name the variables "totalEdges" and "totalTrials." (Add the code for the running average after the rest of the project is operational doing things a little bit at a time is key to rapidly generating bug free code.)
Use the modularization approach in developing a solution to Hank. Four or more procedures will help make the code tractable. Implement and test the procedures individually, before developing the rest of the code (it's called incremental testing). Do not use any global variables to pass variables into or out of functions or subroutines. The procedures should include:
* If you wish, you can replace these two functions with a single procedure
(perhaps a function) called
Numbering your corners as they are numbered in the figure below may help.
Insufficiently commented code, poorly chosen variable or function names, not using functions, or not passing the correct information into a function will be graded harshly. The program requires a block comment at the top stating your name, the date, the program name and program description or purpose. Each function requires a block comment at its top with one (or more if necessary) sentence(s) stating the exact purpose of that function. You may want to indicate incoming and outgoing parameters.
Include a flowchart or pseudocode detailing your solution. It can be a hand-drawn, rough draft and does not have to be fancy.