TWISTER 2: Read on....

Discussion in 'C Programming' started by Nirjhar Oberoi, Nov 24, 2006.

  1. TWISTER 2.1
    ¿? Your have to take a decision in a program but the creator of the
    programming language forgot to supply the programmer with a IF-ELE
    Construct? Can you find a work arround?

    TWISTER 2.2
    ¿? You have to use loops and print 1 to 100 without using any kind of
    looping not even GOTO statement? Can u do it?
    (* *)
    ******************************0oo*********(_)******oo0**************************

    SMALLEST CODE WILL BE APPRECIATED

    :)
     
    Nirjhar Oberoi, Nov 24, 2006
    #1
    1. Advertising

  2. Nirjhar Oberoi said:

    > TWISTER 2.1
    > ¿? Your have to take a decision in a program but the creator of the
    > programming language forgot to supply the programmer with a IF-ELE
    > Construct? Can you find a work arround?


    That depends on what you think IF-ELE should do. Personally, I think it
    should subtract the value of ELE from the value of IF, yielding the
    difference between the two. The workaround would be as follows:

    diff = IF;
    diff -= ELE;

    > TWISTER 2.2
    > ¿? You have to use loops and print 1 to 100 without using any kind of
    > looping not even GOTO statement? Can u do it?


    I don't think he can. I don't think anyone else can either, since using
    loops without using any kind of looping is likely to challenge the
    ingenuity of even the most perspicacious programmer. Nevertheless, printing
    1 to 100 without using any kind of looping is trivial:

    puts("1 to 100");

    > SMALLEST CODE WILL BE APPRECIATED


    No problem. Here ya go:

    main(){}

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 24, 2006
    #2
    1. Advertising

  3. Nirjhar Oberoi

    CBFalconer Guest

    Richard Heathfield wrote:
    > Nirjhar Oberoi said:
    >

    .... snip ...
    >
    >> ¿? You have to use loops and print 1 to 100 without using any
    >> kind of looping not even GOTO statement? Can u do it?

    >
    > I don't think he can. I don't think anyone else can either, since
    > using loops without using any kind of looping is likely to
    > challenge the ingenuity of even the most perspicacious programmer.
    > Nevertheless, printing 1 to 100 without using any kind of looping
    > is trivial:
    >
    > puts("1 to 100");


    You forgot about recursion:

    #include <stdio.h>

    void putn(int n) {
    if (n - 1) putn(n - 1);
    printf("%d\n", n);
    }

    int main(void) {putn(100); return 0;}

    which even the all seeing u can use. Why do people give their
    children one character names?

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Nov 24, 2006
    #3
  4. Nirjhar Oberoi

    Zara Guest

    On 23 Nov 2006 21:40:06 -0800, "Nirjhar Oberoi" <>
    wrote:

    >TWISTER 2.1
    > ¿? Your have to take a decision in a program but the creator of the
    >programming language forgot to supply the programmer with a IF-ELE
    >Construct? Can you find a work arround?


    You may take a peek at local newspapaers, probably ther is some work
    around, where you are not asked to make decisions. Street sweeping?
    Maybe

    >TWISTER 2.2
    > ¿? You have to use loops and print 1 to 100 without using any kind of
    >looping not even GOTO statement? Can u do it?


    Ah! that was VHDL, not C! Off Topic here.

    library loops;
    use loops.all;

    print:std_logic_vector(1 to 100)=(others=>'U');

    As you can see, not only U, but also OTHERS can do it!

    > SMALLEST CODE WILL BE APPRECIATED


    Try with Huffman coding.

    Regards,

    Zara
     
    Zara, Nov 24, 2006
    #4
  5. CBFalconer said:

    > You forgot about recursion:


    No, I didn't. I was saving that in case he turned out to be serious, and to
    provide some effort of his own.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 24, 2006
    #5
  6. Nirjhar Oberoi

    CBFalconer Guest

    Richard Heathfield wrote:
    > CBFalconer said:
    >
    >> You forgot about recursion:

    >
    > No, I didn't. I was saving that in case he turned out to be serious,
    > and to provide some effort of his own.


    Well, I kept the example subtly obtuse (the output order and end
    points) to give him something to think about. However I think
    "challenge the ingenuity of the most perspicacious" implies
    impossibility or the need for non-standard code.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Nov 24, 2006
    #6
  7. CBFalconer said:

    > Richard Heathfield wrote:
    >> CBFalconer said:
    >>
    >>> You forgot about recursion:

    >>
    >> No, I didn't. I was saving that in case he turned out to be serious,
    >> and to provide some effort of his own.

    >
    > Well, I kept the example subtly obtuse (the output order and end
    > points) to give him something to think about. However I think
    > "challenge the ingenuity of the most perspicacious" implies
    > impossibility or the need for non-standard code.


    Yes, I implied impossibility, and I stand by that. You can't use loops
    without using loops, which is what he asked. A recursive call is not a loop
    per se. Nevertheless, I agree that it is probably what his tutor had in
    mind.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 24, 2006
    #7
  8. Nirjhar Oberoi

    Eric Sosman Guest

    CBFalconer wrote:

    > Richard Heathfield wrote:
    >
    >>CBFalconer said:
    >>
    >>
    >>>You forgot about recursion:

    >>
    >>No, I didn't. I was saving that in case he turned out to be serious,
    >>and to provide some effort of his own.

    >
    >
    > Well, I kept the example subtly obtuse (the output order and end
    > points) to give him something to think about. However I think
    > "challenge the ingenuity of the most perspicacious" implies
    > impossibility or the need for non-standard code.


    The original problem statement, in full, was

    > ¿? You have to use loops and print 1 to 100 without using any kind of
    > looping not even GOTO statement? Can u do it?


    .... and neither CBF's nor RH's code meets the requirement to
    "use loops." I offer the following as a possible solution:

    #include <stdio.h>
    int main(void) {
    char buff[293], *p = buff;
    int n;
    for (n = 1; n <= 100; ++n) {
    sprintf(p, "%d\n", n);
    while (*p)
    ++p;
    }
    fputs (buff, stdout);
    return 0;
    }

    This program

    - Uses loops (two of them; it could have been simpler were
    "loops" not in the plural)

    - "and"

    - Prints 1 to 100 without using any kind of looping (the
    output is generated by one execution of an fputs() call).

    However, even this doesn't answer the question. It doesn't matter
    if I or CBF or RH can produce a solution; the question asks whether
    the former Secretary General can do it. Anybody know how to get
    in touch with him?

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 24, 2006
    #8
  9. Eric Sosman said:

    <snip>
    >
    > The original problem statement, in full, was
    >
    > > ¿? You have to use loops and print 1 to 100 without using any kind of
    > > looping not even GOTO statement? Can u do it?

    >
    > ... and neither CBF's nor RH's code meets the requirement to
    > "use loops."


    ....and yours doesn't meet the requirement *not* to use "any kind of
    looping".

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 24, 2006
    #9
  10. Richard Heathfield wrote:
    > Eric Sosman said:
    >
    > <snip>
    > >
    > > The original problem statement, in full, was
    > >
    > > > ¿? You have to use loops and print 1 to 100 without using any kind of
    > > > looping not even GOTO statement? Can u do it?

    > >
    > > ... and neither CBF's nor RH's code meets the requirement to
    > > "use loops."

    >
    > ...and yours doesn't meet the requirement *not* to use "any kind of
    > looping".


    There is no such requirement. There is a requirement to print 1 to 100
    without using any kind of looping. Eric Sosman's code uses a loop, but
    not for the actual printing.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Nov 24, 2006
    #10
  11. Nirjhar Oberoi

    Eric Sosman Guest

    Richard Heathfield wrote:

    > Eric Sosman said:
    >
    > <snip>
    >
    >> The original problem statement, in full, was
    >>
    >> > ¿? You have to use loops and print 1 to 100 without using any kind of
    >> > looping not even GOTO statement? Can u do it?

    >>
    >>... and neither CBF's nor RH's code meets the requirement to
    >>"use loops."

    >
    >
    > ...and yours doesn't meet the requirement *not* to use "any kind of
    > looping".


    It "print 1 to 100 without using any kind of looping."
    All the loops are over and done with before the printing starts,
    and the printing uses no loops. To look at it another way, I'm
    choosing to parse the problem statement with a low precedence
    assigned to "and."

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 24, 2006
    #11
  12. Eric Sosman said:

    <snip>

    > It "print 1 to 100 without using any kind of looping."
    > All the loops are over and done with before the printing starts,
    > and the printing uses no loops. To look at it another way, I'm
    > choosing to parse the problem statement with a low precedence
    > assigned to "and."


    Those 0s in "100" look suspiciously like loops to me! :)


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 24, 2006
    #12
  13. In article <>,
    Richard Heathfield <> wrote:

    >> ... and neither CBF's nor RH's code meets the requirement to
    >> "use loops."


    >...and yours doesn't meet the requirement *not* to use "any kind of
    >looping".


    char *loops = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "
    "21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 "
    "41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 "
    "61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 "
    "81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100";
    printf("%s\n", loops);

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Nov 24, 2006
    #13
  14. Richard Tobin said:

    > In article <>,
    > Richard Heathfield <> wrote:
    >
    >>> ... and neither CBF's nor RH's code meets the requirement to
    >>> "use loops."

    >
    >>...and yours doesn't meet the requirement *not* to use "any kind of
    >>looping".

    >
    > char *loops = [...]
    > printf("%s\n", loops);


    This fails to meet the req - oh, you SOAB. :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 24, 2006
    #14
  15. Nirjhar Oberoi

    Lew Pitcher Guest

    Nirjhar Oberoi wrote:
    > TWISTER 2.1
    > ¿? Your have to take a decision in a program but the creator of the
    > programming language forgot to supply the programmer with a IF-ELE
    > Construct? Can you find a work arround?


    Sure I can. Can't you? What do you think the trinary operator is used
    for if not evaluating conditions? And that's just the most esoteric
    one; there are also for(), while() and do() operations that could also
    substitute for an if-else construct with some work.

    OTOH, I'd fire any language developer who neglected to include if/else
    in his C compiler, wouldn't you?



    > TWISTER 2.2
    > ¿? You have to use loops and print 1 to 100 without using any kind of
    > looping not even GOTO statement? Can u do it?


    Of course I can.

    The simplest way is to just
    puts("1 2 3 4 5 6 7 8 9 10");...
    until you print all 99 of your numbers.

    There are other, fancier ways, of course.
     
    Lew Pitcher, Nov 24, 2006
    #15
  16. Consider this code and give ur Opinion

    Code written in Turbo C
    ------------------------------------------------
    #include <stdio.h>
    #include <conio.h>

    static int i = 1;

    void foo() {
    if (i > 100)
    return;
    else {
    printf("%d ",i);
    foo();
    }
    }

    int main(void) {
    foo();
    }
    -------------------------------------------------

    What say? will it work????

    Ofcourse!!!!
     
    Nirjhar Oberoi, Nov 24, 2006
    #16
  17. Nirjhar Oberoi said:

    > Consider this code and give ur Opinion
    >
    > Code written in Turbo C


    Well, C, pretty much, if you just lose the conio.h header, which is
    non-standard and which you don't need.

    > ------------------------------------------------
    > #include <stdio.h>
    > #include <conio.h>
    >
    > static int i = 1;
    >
    > void foo() {
    > if (i > 100)
    > return;
    > else {
    > printf("%d ",i);
    > foo();
    > }
    > }
    >
    > int main(void) {
    > foo();
    > }
    > -------------------------------------------------
    >
    > What say? will it work????


    No.

    >
    > Ofcourse!!!!


    Of course not. Try it!

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Nov 24, 2006
    #17
  18. Nirjhar Oberoi

    santosh Guest

    Nirjhar Oberoi wrote:

    Please quote the message to which you're replying.

    > Consider this code and give ur Opinion
    >
    > Code written in Turbo C
    > ------------------------------------------------
    > #include <stdio.h>
    > #include <conio.h>
    >
    > static int i = 1;
    >
    > void foo() {
    > if (i > 100)
    > return;
    > else {
    > printf("%d ",i);
    > foo();
    > }
    > }
    >
    > int main(void) {
    > foo();
    > }
    > -------------------------------------------------
    >
    > What say? will it work????
    >
    > Ofcourse!!!!


    No, it won't. Compile it and see.
     
    santosh, Nov 24, 2006
    #18
  19. Nirjhar Oberoi

    Lew Pitcher Guest

    Nirjhar Oberoi wrote:
    > TWISTER 2.1
    > ¿? Your have to take a decision in a program but the creator of the
    > programming language forgot to supply the programmer with a IF-ELE
    > Construct? Can you find a work arround?
    >
    > TWISTER 2.2
    > ¿? You have to use loops and print 1 to 100 without using any kind of
    > looping not even GOTO statement? Can u do it?


    OK, now make certain I get my share of your marks.

    #include <stdio.h>
    void CountTo(int n)
    {
    (n-1)?CountTo(n-1):0; printf("%d\n",n);
    }

    int main(void)
    {
    CountTo(100);
    return 0;
    }
     
    Lew Pitcher, Nov 24, 2006
    #19
  20. Nirjhar Oberoi

    CBFalconer Guest

    Nirjhar Oberoi wrote:
    >
    > Consider this code and give ur Opinion


    Ur was an ancient city in Mesopotamia. I don't think the
    inhabitants ever heard of the C language. Makes it hard for the
    city to have an opinion.

    >
    > Code written in Turbo C


    So what?

    > ------------------------------------------------
    > #include <stdio.h>
    > #include <conio.h>


    Non-standard include. No such thing.
    >
    > static int i = 1;
    >
    > void foo() {
    > if (i > 100)
    > return;
    > else {
    > printf("%d ",i);
    > foo();
    > }
    > }
    >
    > int main(void) {
    > foo();
    > }


    Failure to return a value. At least you defined main correctly.
    With the corrections indicated the program will probably print "1"
    some number of times and then crash. Or it might assassinate all
    males over 15. Neither of these is especially desirable.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Nov 24, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin

    Twister + Lancelot

    ALuPin, Sep 16, 2004, in forum: VHDL
    Replies:
    3
    Views:
    630
    H. Peter Anvin
    Sep 21, 2004
  2. Scott Robert Ladd
    Replies:
    31
    Views:
    6,431
    Carsten Hansen
    Jan 7, 2004
  3. Simon

    Another Mersenne Twister question

    Simon, Oct 25, 2006, in forum: C Programming
    Replies:
    11
    Views:
    607
    Ben Pfaff
    Oct 26, 2006
  4. Nirjhar Oberoi

    TWISTER: Add 2 numbers using "SINGLE" variable

    Nirjhar Oberoi, Nov 23, 2006, in forum: C Programming
    Replies:
    7
    Views:
    1,221
    mdmuzammil
    Aug 10, 2010
  5. Fast Mersenne Twister

    , May 15, 2008, in forum: Python
    Replies:
    0
    Views:
    379
Loading...

Share This Page