Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Yet another fun C-puzzle

Yet another fun C-puzzle

Scheduled Pinned Locked Moved The Lounge
questionc++
32 Posts 14 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Russell Morris

    Hmm...

    static int i=1;
    typedef void (*fn) ();
    fn pfn[2];

    void print_i(void)
    {
    printf("%d\n",i);
    // Not a conditional! :)
    // i++ / 100 will evaluate to 0 until i has reached 100
    (pfn[i++ / 100])();
    }

    void go_bye_bye()
    {
    exit(0);
    }

    void main(void)
    {
    pfn[0] = &print_i;
    pfn[1] = &go_bye_bye;

    (pfn\[0\])();
    

    }

    -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

    D Offline
    D Offline
    David Stone
    wrote on last edited by
    #11

    Very nice. :) I hadn't even considered using function pointers. I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. ;)


    [Cheshire] I can't afford those plastic things to cover the electric sockets so I just draw bunny faces on the electric outlets to scare the kids away from them... [RLtim] Newsflash! Kids aren't afraid of bunnies. [Cheshire] Oh they will be... -Bash.org

    N 1 Reply Last reply
    0
    • D David Stone

      Very nice. :) I hadn't even considered using function pointers. I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. ;)


      [Cheshire] I can't afford those plastic things to cover the electric sockets so I just draw bunny faces on the electric outlets to scare the kids away from them... [RLtim] Newsflash! Kids aren't afraid of bunnies. [Cheshire] Oh they will be... -Bash.org

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #12

      David Stone wrote: I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. Guys, it's okay, David is from Southern California - so as I said, it's quite okay :rolleyes:

      1 Reply Last reply
      0
      • R Russell Morris

        Hmm...

        static int i=1;
        typedef void (*fn) ();
        fn pfn[2];

        void print_i(void)
        {
        printf("%d\n",i);
        // Not a conditional! :)
        // i++ / 100 will evaluate to 0 until i has reached 100
        (pfn[i++ / 100])();
        }

        void go_bye_bye()
        {
        exit(0);
        }

        void main(void)
        {
        pfn[0] = &print_i;
        pfn[1] = &go_bye_bye;

        (pfn\[0\])();
        

        }

        -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #13

        Mine was pretty similar except it doesn't use globals and uses a double-not rather than / 100 :)

        #include <stdlib.h>
        #include <stdio.h>

        typedef void (*funcptr)(int);

        funcptr buf[2];

        void count(int num)
        {
        printf("%d\n", 100-num);
        (*buf[!!num])(num-1);
        }

        int main()
        {
        buf[0] = exit;
        buf[1] = count;
        count(99);
        }

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        N 1 Reply Last reply
        0
        • C Chris Maunder

          Can we print more than 100? Does the program have to terminate elegantly? I came up with a quick solution but it ain't exactly pretty :D cheers, Chris Maunder

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #14

          Chris Maunder wrote: Can we print more than 100? Does the program have to terminate elegantly? I came up with a quick solution but it ain't exactly pretty :-D

          1 Reply Last reply
          0
          • R Ryan Binns

            Mine was pretty similar except it doesn't use globals and uses a double-not rather than / 100 :)

            #include <stdlib.h>
            #include <stdio.h>

            typedef void (*funcptr)(int);

            funcptr buf[2];

            void count(int num)
            {
            printf("%d\n", 100-num);
            (*buf[!!num])(num-1);
            }

            int main()
            {
            buf[0] = exit;
            buf[1] = count;
            count(99);
            }

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #15

            *Applause* The double not was pretty cool - more elegant than Russel's division - and a lot more neater than my comparision operator :-) Nish

            N 1 Reply Last reply
            0
            • N Nish Nishant

              *Applause* The double not was pretty cool - more elegant than Russel's division - and a lot more neater than my comparision operator :-) Nish

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #16

              Ryan Russel still wins though - 1) he posted it first 2) he uses division which while inelegant is not a conditional expression, your double not (while elegant) is still a conditional expression (valid as the question only disallows conditional statements and expressions are not statements) Nish

              1 Reply Last reply
              0
              • N Nish Nishant

                This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                static int i=0;

                class ad
                {
                ad()
                {
                i++;
                cout << i;
                }
                }

                void main()
                {
                ad objad[100];
                }

                I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                V Offline
                V Offline
                Vivek Rajan
                wrote on last edited by
                #17

                How about this int main(int ac, char ** av) { static int i=1; printf("%d ",i++); i%101&&main(0,0); }

                N 1 Reply Last reply
                0
                • V Vivek Rajan

                  How about this int main(int ac, char ** av) { static int i=1; printf("%d ",i++); i%101&&main(0,0); }

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #18

                  Cool stuff, Vivek :-) Nish

                  N 1 Reply Last reply
                  0
                  • N Nish Nishant

                    Cool stuff, Vivek :-) Nish

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #19

                    Here's an improvement (LOC reduced anyway)

                    int main(int ac, char ** av)
                    {
                    printf("%d ",101-ac,(ac%100)&&main(ac+1,0));
                    }

                    Run the app without arguments (first time ac will be 1) Nish

                    A 1 Reply Last reply
                    0
                    • N Nish Nishant

                      BTW 100 printfs don't count :rolleyes:

                      G Offline
                      G Offline
                      GizzoF
                      wrote on last edited by
                      #20

                      Nishant S wrote: BTW 100 printfs don't count why not? It is a solution, isn't it?

                      1 Reply Last reply
                      0
                      • N Nish Nishant

                        This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                        static int i=0;

                        class ad
                        {
                        ad()
                        {
                        i++;
                        cout << i;
                        }
                        }

                        void main()
                        {
                        ad objad[100];
                        }

                        I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                        Z Offline
                        Z Offline
                        Zdeslav Vojkovic
                        wrote on last edited by
                        #21

                        template <long val> void printfn() { std::cout << val << "\n"; printfn<val + 1>(); } template <> void printfn<100L>() { std::cout << "100\n"; } int main(int argc, char* argv[]) { printfn<1L>(); system("PAUSE"); return 0; } this works fine in VS.NET 2003 and gcc, but not with VC++ 6.0 which generates wrong specialization. i suppose that it also faster then previous solutions, since it is resolved in compile time.

                        N 1 Reply Last reply
                        0
                        • Z Zdeslav Vojkovic

                          template <long val> void printfn() { std::cout << val << "\n"; printfn<val + 1>(); } template <> void printfn<100L>() { std::cout << "100\n"; } int main(int argc, char* argv[]) { printfn<1L>(); system("PAUSE"); return 0; } this works fine in VS.NET 2003 and gcc, but not with VC++ 6.0 which generates wrong specialization. i suppose that it also faster then previous solutions, since it is resolved in compile time.

                          N Offline
                          N Offline
                          Nish Nishant
                          wrote on last edited by
                          #22

                          Good code, but C does not support templates :-) Nish

                          Z 1 Reply Last reply
                          0
                          • N Nish Nishant

                            Good code, but C does not support templates :-) Nish

                            Z Offline
                            Z Offline
                            Zdeslav Vojkovic
                            wrote on last edited by
                            #23

                            :-O i got carried away by this line from your original post: "An answer was later posted in C++ (and the poster said that it was not possible without C++)."

                            1 Reply Last reply
                            0
                            • N Nish Nishant

                              This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                              static int i=0;

                              class ad
                              {
                              ad()
                              {
                              i++;
                              cout << i;
                              }
                              }

                              void main()
                              {
                              ad objad[100];
                              }

                              I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                              U Offline
                              U Offline
                              utsav_verma
                              wrote on last edited by
                              #24

                              assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output - int main(int x) { printf("%d",x); if(x<100) main(++x); } program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yr

                              N 2 Replies Last reply
                              0
                              • N Nish Nishant

                                This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                                static int i=0;

                                class ad
                                {
                                ad()
                                {
                                i++;
                                cout << i;
                                }
                                }

                                void main()
                                {
                                ad objad[100];
                                }

                                I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                                J Offline
                                J Offline
                                Jason Pease
                                wrote on last edited by
                                #25

                                Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason

                                N 1 Reply Last reply
                                0
                                • U utsav_verma

                                  assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output - int main(int x) { printf("%d",x); if(x<100) main(++x); } program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yr

                                  N Offline
                                  N Offline
                                  Nish Nishant
                                  wrote on last edited by
                                  #26

                                  if is a conditional statement - not allowed.

                                  1 Reply Last reply
                                  0
                                  • N Nish Nishant

                                    BTW 100 printfs don't count :rolleyes:

                                    D Offline
                                    D Offline
                                    DavidNohejl
                                    wrote on last edited by
                                    #27

                                    Does one printf with 100 numbers count? :) sorry if this question was already asked, I didn't look at other posts... David Never forget: "Stay kul and happy" (I.A.)
                                    David's thoughts / dnhsoftware.org / MyHTMLTidy

                                    1 Reply Last reply
                                    0
                                    • U utsav_verma

                                      assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output - int main(int x) { printf("%d",x); if(x<100) main(++x); } program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yr

                                      N Offline
                                      N Offline
                                      Nish Nishant
                                      wrote on last edited by
                                      #28

                                      utsav_verma wrote: int main(int x) { printf("%d",x); if(x<100) main(++x); } program is expected to run from command prompt as filename 0 what do u think?? In addition to "if" being conditional (and therefore you break the rules), there are other bugs in your code. The first argument to main is the number of arguments and if you invoke the prog as "filename 0" as you say, this will be equal to 2 (including the file name). Thus your prog starts printing from 2. 2nd issue is that main should ideally take 2 arguments (though most compilers will let your code compile).

                                      1 Reply Last reply
                                      0
                                      • J Jason Pease

                                        Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason

                                        N Offline
                                        N Offline
                                        Nish Nishant
                                        wrote on last edited by
                                        #29

                                        Jason Pease wrote: Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } :rolleyes:

                                        1 Reply Last reply
                                        0
                                        • N Nish Nishant

                                          This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                                          static int i=0;

                                          class ad
                                          {
                                          ad()
                                          {
                                          i++;
                                          cout << i;
                                          }
                                          }

                                          void main()
                                          {
                                          ad objad[100];
                                          }

                                          I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                                          D Offline
                                          D Offline
                                          DavidNohejl
                                          wrote on last edited by
                                          #30

                                          #define max 100 static int i = 1; bool Print() { print(i++); return ((max - i) && Print()) }
                                          Since we cant use iterative approarch (loops) let's make it recursive. We can't use condition statement (if) so let's use short-circuit expression :) yeah quite late but I had to go visit lectures :( I've solved it on the way there :) David Never forget: "Stay kul and happy" (I.A.)
                                          David's thoughts / dnhsoftware.org / MyHTMLTidy

                                          M 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups