Thread: c puzzle

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    011
    (Although it could be 101 - since there is nothing determining which of the two threads run first, and thus which order the output is, child or parent).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    Code:
    011
    (Although it could be 101 - since there is nothing determining which of the two threads run first, and thus which order the output is, child or parent).

    --
    Mats
    Nope, neither of those is correct.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by QuantumPete View Post
    Nope, neither of those is correct.

    QuantumPete
    If you flush the output, it's 011. If you don't, it's undefined . So your code is undefined.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    only thing certain (apart from death and taxes! ) is that the code spawns four distinct processess. The unbuffered output is:
    Code:
    01010101 /* Unbuffered */
    and buffered output is:
    Code:
    0
    1
    1

  5. #20
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    If u ran the program as it is you get:
    Code:
    01010101
    If the output is redirected to a file, the same result is obtained.


    If you put a \n after "%d" you get:
    Code:
    0
    1
    1
    If the output is redirected to a file, this is obtained:
    Code:
    0
    1
    0
    1
    0
    1

    The output which goes to a terminal is line buffered, if the output contains a newline then the buffer is flushed. If the line is longer than the buffer, it will be flushed sooner.
    The output which goes to a file is fully buffered. This means that it gets flushed only when the buffer gets full even if there are "\n" in the output.
    fork() creates a new process by duplicating the calling process; this implies that the data space of the parent process is copied to the data space of the child process. The data space includes the un-flushed buffers.
    I have stopped reading Stephen King novels. Now I just read C code instead.

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The output depends on when the output is flushed.

    If it's after one character, the output is 011
    If it's after two characters, the output is 0101
    If it's after three characters or termination, the output is 01010101.

    So it may really be any of those.

  7. #22
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    I've found this programming puzzle questions on a site && i thought to share them with you all:

    Q1 Write a "Hello World" program in 'C' without using a semicolon.
    Q2 Write a C++ program without using any loop (if, for, while etc) to
    print numbers from 1 to 100 and 100 to 1;
    Q3 C/C++ : Exchange two numbers without using a temporary variable.
    Q4 C/C++ : Find if the given number is a power of 2.
    Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
    Q6 C/C++ : Write a function in different ways that will return f(7) =
    4 and f(4) = 7
    Q7 Remove duplicates in array
    Q8 Finding if there is any loop inside linked list.
    Q9 Remove duplicates in an no key access database without using an
    array
    Q10 Write a program whose printed output is an exact copy of the
    source. Needless to say, merely echoing the actual source file is not
    allowed.
    Q11 From a 'pool' of numbers (four '1's, four '2's .... four '6's),
    each player selects a number and adds it to the total. Once a number
    is used, it must be removed from the pool. The winner is the person
    whose number makes the total equal 31 exactly.
    Q12 Swap two numbers without using a third variable.
    Given an array (group) of numbers write all the possible sub groups of
    this group.
    Q14 Convert (integer) number in binary without loops.
    I have stopped reading Stephen King novels. Now I just read C code instead.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I only bothered to do the quine :P. Quite easy tasks, but I like the self-printing programs. I wrote this one before, so I redid what I memorized:

    Code:
    #define A(x)#x;x
    a=A(main(){printf("#define A(x)#x;x\na=A(%s)",a);})

  9. #24
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by ralu. View Post
    Q1 Write a "Hello World" program in 'C' without using a semicolon.
    Note that C99 doesn't require "return" explicitly:

    Code:
    #include <stdio.h>
    int main() 
    {
            if(printf("Hello World!\n")) { }
    }


    Q2 Write a C++ program without using any loop (if, for, while etc) to
    print numbers from 1 to 100 and 100 to 1;
    Code:
    cout << "1 2 3 4 ... 99 100" << endl;
    cout << "100 99 ... 4 3 2 1" << endl;


    Q3 C/C++ : Exchange two numbers without using a temporary variable.
    Note that you may not abbreviate the following:

    Code:
    int a, b;
    
    a ^= b;
    b ^= a;
    a ^= b;


    Q4 C/C++ : Find if the given number is a power of 2.
    Bit twiddling or simply the following:

    Code:
    log2(x) == floor(log2(x))


    Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
    Code:
    x = x+x+x+x+x+x+x;
    /* or */
    x = (x<<3) - x;


    Q6 C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7
    Code:
    /* the obvious approach or this one: */
    int f(int x)
    {
            return x ^ 3;
    }


    Code:
    Q7 Remove duplicates in array
    Where's the puzzle?



    Q8 Finding if there is any loop inside linked list.
    Haire-and-Tortoise approach / Classic Floyd's Cycle Finding Algorithm: Two pointers "slow" and "fast" pointing to the first element. "slow" advances one element per iteration, "fast" advances two. If slow == fast before the end is reached, the list contains a cycle.



    Q9 Remove duplicates in an no key access database without using an array
    See Q7.



    Q10 Write a program whose printed output is an exact copy of the source. Needless to say, merely echoing the actual source file is not allowed.
    Or better: write a program in C that prints a program in Perl that prints the original C program. Once done, this is easily extended to an arbitrary number of steps.



    Q11 From a 'pool' of numbers (four '1's, four '2's .... four '6's),
    each player selects a number and adds it to the total. Once a number
    is used, it must be removed from the pool. The winner is the person
    whose number makes the total equal 31 exactly.
    Too much overhead. Any volunteers?



    Q12 Swap two numbers without using a third variable.
    Solved in Q3.



    Given an array (group) of numbers write all the possible sub groups of this group.
    Where's the puzzle?



    Q14 Convert (integer) number in binary without loops.
    Code:
    x;

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  10. #25
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    > Or better: write a program in C that prints a program in Perl that prints the original C program. Once done, this is easily extended to an arbitrary number of steps.

    Copy that...
    Code:
    #define A(x)#x;x
    a=A(main(){printf("print %c#define A(x)#x;x\na=A(%s)%c;",39,a,39);})
    About the bit twiddling in the power of 2 question:
    bool power_of_two(unsigned int n) { return n && (n & (n-1)) == 0; }

  11. #26
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by QuantumPete View Post
    How about this for a puzzle. What does the following print out on your console and more importantly, why?

    Code:
    #include <stdio.h>
    int main (void)
    {
        int n = 0;
        for (n = 0; n <= 1; ++n) 
        {
            printf ("%d", n);
            fork();
        }
        return 0;
    }
    QuantumPete
    As to why the output is 01010101 when it's buffered; fork() duplicates the stdout buffer in the child process and their exit is equivalent to calling fflush(stdout) in each of the four distinct processes. That's why the buffer containing "01" is printed out 4 times. When stdout is flushed, the same program prints out 011.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 01:53 PM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. Solution to Google Puzzle 3,3,8,8=24
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:12 AM