# Thread: Calculating total distance travelled from time and varying speed

1. ## 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

2. Work out the distance travelled every second, and keep a total of all those results.

3. 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

`total_distance += distance;`