Friday, October 06, 2006

 

Solutions to Chapter 5, p. 92, #3

Question #3 is the only question at the end of the chapter that has a twist or two. It reads:
"Write a program that inputs the starting time and finishing time of an automobile trip as well as the distance in kilometers traveled and outputs the average speed in km/hr. The times are to be given in two parts: the hours, then the minutes."


Begin by trying to deal with the simplest case - for example, a trip that starts at 8 a.m., ends at 10 a.m. and is a distance of 100 km. To begin with you only need two variables - let's call them startHour and endHour - to find how long the trip lasted. So endHour minus startHour gives you the trip in hours. You need another variable called, say, distance. Then to find the average speed in km per hour, divide the hours by the number of km. The Turing code will look like this.


var startHour, endHour :int
var distance : real
put "Enter hour when trip started: "
get startHour
put "Enter hour when trip ended: "
get endHour
put "Enter distance of trip: "
get distance
put "Average speed of trip was ", (endHour - startHour) / distance

But trips don't always begin and end exactly on the hour. How do we deal with minutes? Convert all the times into minutes, then subtract to give you the time of the trip in minutes. The textbook indicates this method by saying, "the times are to be given in two parts: the hours, then the minutes." Next, you want the result in km per hour. That means distance divided by time (km per hr). But since your time at this point is still in minutes, you divide distance by time then divide the result by 60 to get km per hour. Look carefully at the expression below, especially the brackets.

var startMin, startHour, endMin, endHour :int
var distance : real
put "Enter hour when trip started: "
get startHour
put "Enter minute when trip started: "
get startMin
put "Enter hour when trip ended: "
get endHour
put "Enter minute when trip ended: "
get endMin
put "Enter distance of trip: "
get distance
put "Average speed of trip was ", distance / (((endHour * 60 + endMin) - (startHour * 60 + startMin)) /60), " km / hr"

What if the trip starts in the morning, say 11 a.m., and ends later in the day, say, 4 p.m.? The number 11 is larger than 4. The way to get around this is to ask the user to enter hours using a 24-hour clock:

put "Enter hour when trip started (use a 24-hour clock): "
get startHour
. . .
put "Enter hour when trip ended (use a 24-hour clock): "
get endHour
. . .

The final twist to this problem is when the trip lasts beyond midnight, for example from 9 p.m. to 3 a.m. (that is 21 hours to 3 hours). Use an if construct as below. We won't be studying if constructs until Chapter 9, but some of you are already starting to get the idea. Don't worry if this part is confusing. You will learn about it later in the course.

. . .
get endHour
if endHour < startHour then
endHour := endHour + 24
end if
. . .




<< Home

This page is powered by Blogger. Isn't yours?