km/h calculator
Author |
Message |
chroncile
|
Posted: Sat Nov 22, 2008 7:23 pm Post subject: km/h calculator |
|
|
Hi, I have to do this for school and I have tried for hours figuring out how to do it, but so far I have failed.
The assignment asks for this:
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.
Here is my code so far:
Quote:
var StartHour : real
var FinishHour : real
var DistanceKm : int
var StartMin : int
var FinMin : int
put "Use a space to seperate the hours from the minutes"
put "When did you start driving? "..
get StartHour
get StartMin
put "At what time did you arrive? "..
get FinishHour
get FinMin
put "How far away was your destination from the starting point(km)? "..
get DistanceKm
put DistanceKm * ((FinishHour + FinMin) - (StartHour + StartMin)), " km/h"
If you were to put in 8 30 and 9 30, it would calculate the right speed, but any other number will be incorrect. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
cavetroll
|
Posted: Sat Nov 22, 2008 8:36 pm Post subject: RE:km/h calculator |
|
|
The problem is that you are adding a value of 1 to represent an hour. An hour is sixty minutes, so when adding the hours and minutes together, you need to either multiply the hours by 60, or divide the minutes by 60. This should give a more accurate result. |
|
|
|
|
|
chroncile
|
Posted: Sat Nov 22, 2008 9:25 pm Post subject: RE:km/h calculator |
|
|
I have tried doing this instead:
put DistanceKm * ((FinishHour * 60 + FinMin) - (StartHour * 60 + StartMin)), " km/h" but it doesn't work |
|
|
|
|
|
cavetroll
|
Posted: Sat Nov 22, 2008 9:52 pm Post subject: Re: km/h calculator |
|
|
Sorry about that, I didn't actually test my recommendation. I do see where the problem is now.
Km/h is equal to the number of km's divided by the number of hours. You are multiplying the number of km's by the number of hours. Also, keep in mind that after you are done calculating, you will be at a number that is in km/min, so you need to multiply the final result by 1/[number of minutes in an hour] (or divide by 60). |
|
|
|
|
|
DanielG
|
Posted: Sat Nov 22, 2008 11:42 pm Post subject: RE:km/h calculator |
|
|
chroncile, what you wrote above is correct, only you need to change the * to divide bcecause it is kilometers per (meaning divide) hour. so it should be more like put DistKm/(EndTime-StartTime). |
|
|
|
|
|
chroncile
|
Posted: Sat Nov 22, 2008 11:45 pm Post subject: RE:km/h calculator |
|
|
It works sometimes, but not always;
Quote:
Use a space to seperate the hours from the minutes
When did you start driving? 8 15
At what time did you arrive? 9 10
How far away was your destination from the starting point(km)? 45
-11.25 km/h
I don't understand why it's doing that |
|
|
|
|
|
cavetroll
|
Posted: Sun Nov 23, 2008 12:01 am Post subject: Re: km/h calculator |
|
|
For the results you posted, what is the line of code you are using? This is why you need the times 60.
code: |
=45/((9+10)-(8+15))
=45/(19-23)
=45/-4
=-11.25
|
If instead of that, you use the *60 part, it will work.
code: |
=(45/((9*60+10)-(8*60+15)))*60
=(45/((540+15)-(480+15)))*60
=(45/(550-495))*60
=(45/55)*60
=49.1 km/h
|
I realize now that my other messages weren't very clear. Sorry. Hopefully this clarifies what I was attempting to explain. |
|
|
|
|
|
chroncile
|
Posted: Sun Nov 23, 2008 12:06 pm Post subject: RE:km/h calculator |
|
|
Thank you, that did the trick |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|