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.
  • 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
                                    • D DavidNohejl

                                      #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 Offline
                                      M Offline
                                      mracoder
                                      wrote on last edited by
                                      #31

                                      Well thank you wery much David for enlightening me with your approach to the problem!!! - Finally I've got it what the last line (like in Vivek Ryan's code, for example) actually does! I persume, now, that C doesn't run/check the second argument of AND(&&) when the first is already evaluated as "bogus". Nice trick, must say! MY GREETINGS TO ALL by mladjan

                                      1 Reply Last reply
                                      0
                                      • N Nish Nishant

                                        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 Offline
                                        A Offline
                                        Andrew N
                                        wrote on last edited by
                                        #32

                                        Because logical operators were not allowed, let's rewrite your code just a bit: int main(int i) { printf("%d ", i, 1/(i-101)); main(++i); }

                                        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