Lab 5 Details: -- Automobile Traffic Simulation with Interactions
COM1101 Winter 2001

(Version of 2/5/2001)

Professor Futrelle
College of Computer Science, Northeastern U., Boston, MA

This page explains a few of the technical details of the simulation and code organization for this lab. The assignment, what you are to hand in, is described in the assignment page. The lab will take place on Tuesday, February 6th and is due by 4pm on Monday, February 12th. Further details to help you along will be added to this note over the next few days and you will be notified in class and by email of any additions.

Much of what you are to do is explained in the basic assignment page as well as the pages referred to there, especially the page describing the API for the one-file solution that I developed for the complete project, covering Labs 5 and 6.

NEW NOTE ON POINTERS AND ARRAYS OF POINTERS -- FRIDAY FEB. 8TH

See this page for examples of how to call functions with arguments that are pointers to objects or arrays of pointers to objects. It also shows how to return a pointer to an object as the value of a function. One of the most convenient uses of pointers is to pass a pointer to a function, and have the function modify the object. The function doesn't have to "return" anything, since the pointer you gave the function still points at the object. The only difference is that some values inside the object may have been changed -- and if this is what you want to do it is simple to do. (Reference arguments in C++ are unecssary but added for convenience, since they are only a notational variant of the direct passing of pointers. That's why they behave the same way. Java passes everything by reference, so it doesn't need two different sets of notations, actually three in C++, only one -- a "three-to-one" advantage.)


The goal of Lab 5 is to "sketch out" as much of the system as you can, in a single-file or multiple file project. The entire traffic simulation machine problem will be finished up in Lab 6 to be held 2/13 and due 2/19. For Lab 5 you can make heavy use of "stubs" to build compilable code with all member functions present, even if they have empty code bodies, no "guts". Then fill in as many of the stubs with full definitions, testing as you go.

The major difference in this lab is that every car is placed initially at distinct positions and the run program sets up the cars so that each car has a pointer to the car ahead of it. No new cars are added after the run starts and none are removed. The simulation simply runs its set number of times and quits.

The heart of this simulation is the "policy" used by each car to adjust its speed on the basis of its speed and position relative to the car immediately ahead of it. This policy is a set of tests and speed adjustments in the step() member function of each car. I give an example of this below, though it's a lot simpler than the policy I used. In this simple example the speed is increased if the cars are far apart and decreased if they're close together. No allowance is made for any limit on the speed or for preventing the speed from becoming negative. Assume that the we've calculated the actualSeparation, in meters. Then the following statements could be used to adjust the speed once at each iteration of the loop:

if( actualSeparation < OKseparation) speed -= 2.0; // braking is fast
if( actualSeparation > 2*OKseparation) speed += 1.0; // acceleration is slower

Basics of pointers and object creation:

An array of pointers to cars can be declared as,

  Car *myCars[10]; // (not sure if arrays of pointers are in Savitch)

and an element filled using the new operator, Savitch, pgs. 686-7

   myCars[i] = new Car; // once for each element in the array

Accessing any field or member function uses the arrow operator, -> so that a member assignment could be, for example:

  myCars[3]->speed += 1.0;  // arrow operator, Savitch, pgs. 804 and 806

For now, you can just access fields with the arrow operator to set the initial values you need. We will learn about a better way to do this, with constructors, and use these in Lab 6, as I have in my API.

For the Simulation class, you can define a single pointer variable to point to the single instance of Simulation you create, as in the following (from my main()),

	Simulation *sim = new Simulation;
	sim->run(200,1);

The book explains how to use typedef to avoid some of the '*' notation. In Java, Lisp and Scheme, everything is accessed via pointers, so there is no need for special notation.