Speed and Time

Calculate the distance traveled by a car, time spent and average speed

Page 1 of 1

1 Replies - 1209 Views - Last Post: 16 October 2009 - 07:34 AM Rate Topic: -----

#1 Polyp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-October 09

Speed and Time

Posted 16 October 2009 - 04:57 AM

Computer Science Grad '99. About a week ago, I begin my course in 3D videogames programmer untill October 2011.
I have a question and still wasn't sure if this is a corect answer! :blink:

Calculate the distance traveled by a car, time spent and average speed, given the initial and final distance and starting and ending times of travel. It is assumed that the journey begins and ends the same day and time taken (approximate in minutes). using a 24-hour clock. The formula for calculating the speed (v) is: v = e / t where (e) represents the space traversed and (t) time

What should i do next?

#include <iostream>

using namespace std;

float km_inicial;
float km_final;
float minutos_inicial;
float minutos_final;

int main()
	cout << "Introduzca KM inicial: ";
	cin  >> km_inicial;

	cout << "Introduzca KM final: ";
	cin  >> km_final;

	cout << "Introduzca minutos inicial: ";
	cin  >> minutos_inicial;

	cout << "Introduzca minutos final: ";
	cin  >> minutos_final;
	cout << "\n";

	cout << "Distancia en KM: " << (km_inicial - km_final) << "\n";
	cout << "Tiempo en minutos: " << (minutos_inicial - minutos_final) << "\n";

	cout << "Velocidad : " << (km_inicial - km_final) / (minutos_inicial - minutos_final) << "\n\n";

return 0;

Added code tags to your post. When posting code please use tags like below.

Is This A Good Question/Topic? 0
  • +

Replies To: Speed and Time

#2 Aeternalis   User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Speed and Time

Posted 16 October 2009 - 07:34 AM

PLEASE post using the code /code tags!

Not sure what the question is here.. but just looking at your code there are some adjustments you should make.

first of all instead of using the expression
 km_inicial - km_final 
in several places.. what you are really wanting to do is use the RESULT of that expression, so store it in a variable and use the variable instead. Also shouldn't it be final - inicial ? assuming your talking about mile markers or some such..

 float km_result =  km_final - km_inicial; 
cout << "Distancia en KM: " << km_result << "\n";

Also use it in your calculations .. it will make them simpler and less prone to bugs and errors.
same here .. final - inicial right?
float minutos_result = minutos_final - minutos_inicial;
float velocidad = km_result / minutos_result;

cout << "Velocidad : " << velocidad << "\n\n";

This will allow you to validate the data before entering it in your variable for errors such as entering the final time as the inicial time - resulting in a negative time and invalid velocity. Something you might want to check for.

Also, you may run into problems with Units.. The above calculations will give you velocity in Km/minute most people use Km/hour you can convert the minutes to hours by dividing it by 60 before you use it to divide the kilometers..that will give you kilometers per hour like on your car's dashboard, which is a more universal unit in your answer.

Man I hope Im not making some simple math mistakes here :)


This post has been edited by Aeternalis: 16 October 2009 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1