Thread: Tik Tok

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    Tik Tok

    Hey guys,
    I have started exploring the time library, but I can't understand it:
    Code:
    #include<stdio.h>
    #include<time.h>
    int main(void)
    {
        clock_t   h;
        //first assignment 
        h = clock();
        
        while(h != 9)
        {
        //second assignment
        h = clock();
    
        printf("%d\n",h);
        }
        
        system("pause");
        return 0;
    }
    I have got three questions:
    1. If I am not doing first assignment of 'h' than the program is not working properly, why?

    2. What does h gets assigned to? Is it seconds?

    3. How can I make a program to print something for few seconds?

    Any help will be appreciated.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Harith View Post
    Hey guys,
    I have started exploring the time library, but I can't understand it:
    Code:
    #include<stdio.h>
    #include<time.h>
    int main(void)
    {
        clock_t   h;
        //first assignment 
        h = clock();
        
        while(h != 9)
        {
        //second assignment
        h = clock();
    
        printf("%d\n",h);
        }
        
        system("pause");
        return 0;
    }
    I have got three questions:
    1. If I am not doing first assignment of 'h' than the program is not working properly, why?

    2. What does h gets assigned to? Is it seconds?

    3. How can I make a program to print something for few seconds?

    Any help will be appreciated.
    (1) Well, 'h' is uninitialized - what do expect it to contain?
    (2) Depends on the implementation. Refer to the macro CLOCKS_PER_SEC.
    (3) Perhaps this will help get you started:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    void delay( double seconds )
    {
        clock_t
            period = ( clock_t )( clock( ) + CLOCKS_PER_SEC * seconds );
        while( clock( ) < period )
            continue;
    }
    void print_with_delay( const char* message, double seconds )
    {
        size_t
            index;
        for( index = 0; message[ index ] != 0; ++index )
        {
            putchar( message[ index ] );
            fflush( stdout );
            delay( seconds );
        }
    }
    int main( void )
    {
        print_with_delay( "Printing with a delay of 0.2 seconds\n", 0.2 );
        print_with_delay( "Printing with a delay of 0.1 seconds\n", 0.1 );
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Why are your parenthesis like that? It's kind of hard to read.

    Edit: also

    Code:
        while( clock( ) < amount )
            continue;
    Really?!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    clock - C++ Reference

    The value returned is expressed in clock ticks, which are units of time of a constant but system-specific length (with a relation of CLOCKS_PER_SEC clock ticks per second).
    The function reference is written for something...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by SirPrattlepod View Post
    Why are your parenthesis like that? It's kind of hard to read
    I suppose you'd prefer the style used in my signature then?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Sebastiani View Post
    I suppose you'd prefer the style used in my signature then?
    Style in sig is slightly better

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by SirPrattlepod View Post
    Style in sig is slightly better
    [...]
    Edit: also
    [...]
    Really?!
    (1) I could give a flying flip for what you think of my style.
    (2) "sleep" is not standard, if that's what your thinking, which is why I posted the busy loop version.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    [QUOTE=Sebastiani;1179319
    (2) "sleep" is not standard, if that's what your thinking, which is why I posted the busy loop version.[/QUOTE]

    Nah, that's not what I was thinking. I was thinking continue was a bit... errr, umm... strange

    https://www.youtube.com/watch?v=Z6lTcZ5qoUk

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by SirPrattlepod View Post
    Nah, that's not what I was thinking. I was thinking continue was a bit... errr, umm... strange
    SirPrattlepod->View Profile->Add to Ignore List
    [[CLICK]]
    Wow, that was easy...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Tags for this Thread