CPE 101
Laboratory 6 (Complex Loops)


This is an individual activity.   You do not need to create a lab notebook entry for this lab.

Note to returning students: If you previously completed this lab then do Ch 5 Hwk Set 2 #1 and Supplemental Looping Problem #2. Use "handin" and name the programs lab6A.c and lab6B.c.

Part 1 

A car and a train are both travelling toward a railroad crossing on a collision course.  The train is 1200 feet from the crossing and traveling at a constant speed of 40 feet per second.  The car is 1500 feet away and travelling at 55 feet per second.  Write a program named car_train_1.c to calculate and print each vehicles distance from the crossing at one second intervals.  (Don't use the "formula" for distance; instead, substract the distance travelled in one second each time through the loop).  If the car gets to the crossing first, print "Made it."  If the train gets to the crossing first, print "Crash".   (If they arrive at the same time, it's a crash).  The initial distances and speeds  may be "hard coded" in the program.  The results will appear something like this:

Secs    Car     Train
1       1445    1160
2       1390    1120
3       1335    1080
4       1280    1040
.
.
.

Be sure to display the distances until one of the distances is less than or equal to zero.

Part 2

Write another program, car_train_2.c that is a modification of the previous program so that it prints a table of results for car speeds from 10 to 1000 feet per second, at increments of 10.  You should write another loop that surrounds the loop you wrote for part 1.

The results will appear something like this:

Speed     Result
-----     ------
10         Crash
20         Crash
30         Crash
.
.
.
980         Made it
990         Made it
1000        Made it

Part 3: (Optional) 

If you have time, decompose your solution so that the loop you created in part 1 is in a function.  You have to decide what would be the appropriate parameters to this function.   

Part 4: Handing in Your Source Electronically…

1.      Handin the source code files car_train_1.c and car_train_2.c  as you’ve done for previous labs

handin graderjd Lab06 car_train_1.c car_train_2.c