Thread: Timed functions

  1. #1
    Unregistered
    Guest

    Question Timed functions

    I am reading the How to program in C book by Deitel, and at the end of chapter 7, we are supposed to make a program (the source code is not given) that simulates a race between a rabbit and a turtle.
    It says that the animals are going to move through the race, every second. This means that I am goint to have either a function that is executed every second. I know I will have to use time.h, but how do I go about doing this?


    Thanks.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <time.h>
    #include stuff
    
    function prototype
    
    int main()
    {
            while(1)
            {
                    function();
                    wait(1);
            }
            return 0;
    }
    
    void wait( int seconds ) 
    {
        clock_t endtime = clock() + seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
    }
    The world is waiting. I must leave you now.

  3. #3
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    or if you have dos.h, you can use delay(1000) instead of void wait().

    example:
    Code:
    int main()
    {
     while( /* argument(s) */ )
     {
     // code...
     delay(1000);
     }
     return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Since you're only supposed to simulate time, there's no need to complicate it by synchronising this to real time (unless you want to).

    Code:
    for ( seconds = 0 ; seconds < 20 ; seconds++ ) {
      // calculate distance, based on velocity and elapsed time
      // for the rabbit and turtle
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM