School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,137 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,855 people online right now. Registration is fast and FREE... Join Now!




Speed and Time

 

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

Polyp

16 Oct, 2009 - 03:57 AM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2009
Posts: 1


My Contributions
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.gif

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?

CODE

#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";

    system("pause");
return 0;
}


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

User is offlineProfile CardPM
+Quote Post


Aeternalis

RE: Speed And Time

16 Oct, 2009 - 06:34 AM
Post #2

D.I.C Regular
***

Joined: 13 Jul, 2009
Posts: 273



Thanked: 25 times
My Contributions
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
CODE
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..

CODE
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?
CODE

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 smile.gif

Aet

This post has been edited by Aeternalis: 16 Oct, 2009 - 06:40 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:07PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month