# Calculating total distance travelled from time and varying speed

• 01-07-2006
collin
Calculating total distance travelled from time and varying speed
Hi

I'm writing a program in C which simulates a car dashboard. I've already got a time variable which starts counting as soon as the program starts and continues while ever it is running. I've also already got a speed variable which can be varied ranging from 0-125mph. I need to continuously calculate and display the total distance travelled for the time the program is running, while the speed is constantly changing.

If the speed was constant then it would just be a simple distance = speed x time but because the speed can be varied, if it was reduced, then the distance would reduce and go backwards!

How would i go about calculating the total distance travelled for the length of time the program is running, when the speed is constantly changing?

Thanks
• 01-07-2006
Salem
Work out the distance travelled every second, and keep a total of all those results.
• 01-08-2006
collin
cheers.

I sort of get the theory of how to work it out, i'm just not very good at programming so unsure of the code.

i think i understand how to program calculating the distance every second:

distance = speed x 0.00278 which is in a loop that repeats every second and calculates the distance every second in miles.

the only bit i'm not sure about is constantly adding all these values together?

total_distance = ???

Thanks
• 01-08-2006
ZuK
Code:

`total_distance += distance;`
• 01-08-2006
collin
that would work then? Like i say i'm not very good at programming at all, just got to be done at uni! I thought it was going to be some sort of loop i needed.
• 01-08-2006
ZuK
this would work if it is placed in a loop that runs once every second.
Kurt
• 01-08-2006
collin
excellent, i'll give that a try at uni tomorrow then.

Thanks!