IonicWind Software

Creative Basic => Console Programs => Topic started by: GWS on September 10, 2015, 02:55:11 AM

Title: Speed - Distance Problem
Post by: GWS on September 10, 2015, 02:55:11 AM
Hi,

I don't know why I bothered with this, except it is a coding problem, and so needs solving just so it doesn't beat me ..  :)

It comes from a very old bit of code in Creative Computing many moons ago ..

The problem as stated is:

A Car travelling at an average speed of C mph, can make a trip in H hours less than a Train
travelling at T mph. How long does the trip take by car ?
(The train speed includes lost time due to connections.  Thus C is always greater than T).


' Speed and Time problem
' A Car travelling at C mph, can make a trip in H hours less than a Train
' travelling at T mph. How long does the trip take by car
' The train speed includes lost time due to connections.  Thus C is always greater than T.

openconsole

def C,T,H:int
def Error,EstCarTime,CarTime:float
def YesNo:string

Label Another

cls

PRINT: PRINT "TIME - SPEED - DISTANCE Exercise": PRINT
PRINT string$(32,"-")

Label Redo

C =INT(50*RND(1)) + 35 :' Car Speed
T =INT(30*RND(1)) + 15 :' Train Speed
H =INT(10*RND(1)) + 5 :' Time Difference

' make sure the car speed is greater than the train speed
if (C <= T) then goto Redo

PRINT
PRINT "A Car travelling at ",C,"mph can make a trip in"
PRINT H,"hours less than a train travelling at ",T,"mph."
PRINT
INPUT "How long does the trip take by car ?  ",EstCarTime

CarTime = H * T /(C-T)

Error = INT(ABS((CarTime-EstCarTime)*100/CarTime)+.5)
PRINT:PRINT string$(32,"-")

' check if estimate is more than 5% out ..
if (Error > 5)
PRINT:PRINT "Sorry.  You were off by ",Error,"percent."
PRINT
PRINT "The correct answer is ",CarTime," hours."
PRINT
else
PRINT
PRINT "Good Answer. You were only ",Error,"percent from"
PRINT "the correct answer which is ",CarTime," hours."
PRINT
endif

PRINT string$(32,"-"):PRINT:PRINT
INPUT "Would you like to try again (Y/N) ?  ",YesNo

if (YesNO = "Y") | (YesNo = "y") then goto Another

closeconsole
end

' ........................
' So how does the CarTime calculation come about ..
'
' The distance is unknown, but we start from Speed = Distance / Time
' so car speed C = Distance / CarTime, and train speed T = Distance / TrainTime
' Therefore CarTime = Distance / C, and TrainTime = Distance / T
' Time saving H = TrainTime - CarTime
' = Distance / T - Distance / C
' so H * T = Distance - (Distance / C) * T
' = Distance - CarTime * T
' = C * CarTime - CarTime * T
' = CarTime * (C - T)
' hence, CarTime = H * T / (C - T)

' Phew !! I'm geting too old for this stuff ..

' ........................



There, now I've got a headache ..  ::)

I reckon to use this program you've got to either be a good guesser, or you need to work it out for each set of values - not much fun ..  So I don't rate this very highly either for entertainment value or utility.
Still, I suppose all programs can't be exciting ..  ::)

Best wishes, :)

Graham

Title: Re: Speed - Distance Problem
Post by: aurelCB on September 11, 2015, 01:41:20 PM
Hi GWS
my main problem here is how to recalculate mph into kilometer per hour  ;D
Title: Re: Speed - Distance Problem
Post by: GWS on September 12, 2015, 02:04:14 AM
Hi,

Ah, units conversion - always a problem  ::)

Try typing 'mph2kph' in Google. There are several online calculators to do the conversion.

By the way, the formula for the answer is in the code .. that should enable you to get close to the correct answer ..

all the best, :)

Graham

Title: Re: Speed - Distance Problem
Post by: aurelCB on September 12, 2015, 02:29:12 PM
i know sir...
it is just a joke  ;)
tanks for reply  :)
Title: Re: Speed - Distance Problem
Post by: fasecero on September 13, 2015, 02:22:46 PM
Agreed. Unit conversion are hard to solve but always necessary. Conversion factor can be handy, a quick way to perform multiple conversions in a single operation. There are some examples below

https://en.wikipedia.org/wiki/Dimensional_analysis#The_factor-label_method_for_converting_units (https://en.wikipedia.org/wiki/Dimensional_analysis#The_factor-label_method_for_converting_units)