### Nested Loops and Assignment 5

Nested Loops

In the last set of notes, you were introduced to while loops.  The example that was given there was of a single loop.  Loops can actually be inside of other loops.  This is a useful technique and is called nesting loops.  An example of nested loops is shown below.

while(i<=10)
{
while(j<=10)
{
cout<<i*j<<endl;
j=j+1;
}
i=i+1;
}

This code fragment simply multiplies every number between 1 and 10 with every other number between 1 and 10.  It starts by multiplying 1 and 1, then 1 and 2 and so on until it multiplies 1 and 10.  Then it multiplies 2 and 1, 2 and 2 and so on until it multiplies 2 and 10.  Finally it multiplies 10 and 10.

For more on nested loops read pages 215-220 in A Computer Science Tapestry.

Assignment 5

President Plimpton has been accused of meeting another woman, Monifer Lewones.   Apparently, he was staying at a hotel in New York City.  He allegedly left his room at 4:00 a.m. and took his limousine to Monifer's apartment.  The Presidential limousine is rather conspicuous, so there were many witnesses to this drive.   Seeing the unusual nature of the situation these people noted precisely when and where they saw the limousine.  At Plimptons's trial for this incident, each of the people will testify as to when and where (distance from the hotel) that they saw the limousine.  Plimpton claims that this is untrue and that the times and locations of the witnesses will not check out.  Your job is to confirm or refute this.

The limousine followed a precisely 3 mile route.  The traffic was stop and go, so that the car was only able to accelerate to 30 mph in 1/4 mile before it had to slow down.   This resulted in the car accelerating for 1 minute (60 seconds) and then decelerating for 60 seconds, and then accelerating again.

You will need to simulate this trip by having the car accelerate for 60 seconds and then decelerate for 60 seconds until it has traveled for 3 miles.  You should print out the current time and total distance traveled every 10 seconds.  You have been provided with accelerate and decelerate functions in a file called physics.cpp and physics.h which you must add to your project.  Each function takes one parameter, the time that the car either accelerates or decelerates for.   The function returns the distance that was traveled in that time.  An example using these functions to accelerate the car for 10 seconds and decelerate the car for 10 seconds  is shown here.

#include "physics.h"
#include <iostream.h>
Physics limo;
int main(void)
{
float distance;
distance=0;
distance=distance+limo.accelerate(10);
cout<<"distance= "<<distance<<endl;
distance=distance+limo.decelerate(10);
cout<<"distance="<<distance<<endl;
}

You will need to track total distance and total time passed.  It has been suggested to you that using a while loop to keep track of total distance and nested while loops to keep track of the time spent accelerating and decelerating would be helpful.   You should also use a counter to stop the printout every 20 lines so that it doesn't scroll off the screen.

Once your program is working, check the following times and distances to see if they coincide with the times and distances you calculated (rounded off).

Time=100 Distance= .4722
Time=200 Distance= .8889
Time=300 Distance= 1.25
Time=400 Distance= 1.611
Time=500 Distance= 2.312
Time=600 Distance= 2.5
Time=700 Distance= 2.9722

Do these times coincide with your calculations?  If so, demonstrate it. If not, show which numbers are different.

A working version of the program is available.  But it only prints out for the first 120 seconds so that it doesn't give away the ending.

If you think this problem won't keep you busy long enough, you can write your own accelerate and decelerate functions.  You can find the equations you will need here.

If you think that this problem is beyond you right now, click here for an alternative, but check with Mr. Klopfer before proceeding.

This program will be graded on the following factors:

1. How well it works.