Calculating total distance travelled from time and varying speed

This is a discussion on Calculating total distance travelled from time and varying speed within the C Programming forums, part of the General Programming Boards category; Hi I'm writing a program in C which simulates a car dashboard. I've already got a time variable which starts ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    4

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Work out the distance travelled every second, and keep a total of all those results.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    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

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    what about
    Code:
    total_distance += distance;

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    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.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    this would work if it is placed in a loop that runs once every second.
    Kurt

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    excellent, i'll give that a try at uni tomorrow then.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pthreads performance
    By C_ntua in forum C Programming
    Replies: 42
    Last Post: 06-17-2008, 12:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21