how to calculate the speed of a journey

i have to intup the time periods(12 55; 18 00 etc) and distance from the console for a few journeys and then i have to output the journey with the greatest speed, how do i calculate the speed, is there a function for it?

speed is distance travelled / time taken
What are schools teaching nowadays?
Lemme guess, social media skills and how to be "inclusive" and "diverse" at the same time.

https://en.wikipedia.org/wiki/Newton's_laws_of_motion

i know how to calculate the speed, thanks. i mean if my time of journey looks like "505", how do i correctly calculate the speed without errors?
here's a part of my code that calculates the difference between two time periods i intup
1
2
3
4
5
int first_time_min = stod(f[i].departure) / 100 * 60 + stoi(f[i].departure) % 100;
        int second_time_min = stod(f[i].arrival) / 100 * 60 + stoi(f[i].arrival) % 100;
        int diff_time_min = second_time_min - first_time_min;

        cout << "Time elapsed: " << diff_time_min / 60 << " hours " << diff_time_min % 60 << " minutes" << endl;
Last edited on
So your question was nothing to do with a speed calculation, you're still stuck on the difference between two times.

It's really important to be specific about what you're stuck on, and to post your best effort in the first instance.

You see, it's taken 2 hours of ping-pong just to figure out your real question.

So
minutes_since_midnight = hours * 60 + minutes;

Thus
1
2
departure = 12 * 60 + 55;
arrival = 18 * 60 + 0;
What does '505' represent? What's the format/type of the departure/arrival times? Are the departure/arrival times always within the same 24 hour period?
505 is 5 hours and 5 minutes time difference. i've already found a way to calculate the difference between two time periods, but am stuck on the spead. but i'm gessing i need to find speed in km/minute first and then *60, right?
As others have mentioned, you need to convert the hours/minutes values into decimal minutes, or hours, or seconds. Write a function that does this conversion.

One other thing to consider, what about this trip:
start time: 23:18
end time: 02:23

In this case, the trip ends the following day. To handle this case, after you convert to a decimal value, check if the end time is "before" the start time. If so then add 24 hours to the end time.

Conceptually, you're computing the elapsed time from the start of the day on which the trip began. In other words, from 00:00 on the starting day.
Topic archived. No new replies allowed.