interesting C program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Dollin

    #61
    Re: interesting C program

    Ben Bacarisse wrote:
    I think it also pushes the requirement that "N is a int parameter to
    the function" (sic) a bit far. Still, neither is a fatal flaw to the
    deliciously sneaky idea:
    (fx:snip)
    int print_one(const void *a, const void *b)
    (fx:ditto)
    void print(int n)
    Two functions. In this (sub)thread, one of the Dollins claims
    that's out-of-spec, since the original post called for /a/
    function.

    --
    A /And/ B Hedgehog
    The shortcuts are all full of people using them.

    Comment

    • Ben Bacarisse

      #62
      Re: interesting C program

      Chris Dollin <eh@electriched gehog.netwrites :
      Ben Bacarisse wrote:
      >
      >I think it also pushes the requirement that "N is a int parameter to
      >the function" (sic) a bit far. Still, neither is a fatal flaw to the
      >deliciously sneaky idea:
      >
      (fx:snip)
      >
      >int print_one(const void *a, const void *b)
      >
      (fx:ditto)
      >
      >void print(int n)
      >
      Two functions. In this (sub)thread, one of the Dollins claims
      that's out-of-spec, since the original post called for /a/
      function.
      Harsh, very harsh.

      --
      Ben.

      Comment

      • Chris Dollin

        #63
        Re: interesting C program

        Ben Bacarisse wrote:
        Chris Dollin <eh@electriched gehog.netwrites :
        >
        >Ben Bacarisse wrote:
        >>
        >>I think it also pushes the requirement that "N is a int parameter to
        >>the function" (sic) a bit far. Still, neither is a fatal flaw to the
        >>deliciously sneaky idea:
        >>
        >(fx:snip)
        >>
        >>int print_one(const void *a, const void *b)
        >>
        >(fx:ditto)
        >>
        >>void print(int n)
        >>
        >Two functions. In this (sub)thread, one of the Dollins claims
        >that's out-of-spec, since the original post called for /a/
        >function.
        >
        Harsh, very harsh.
        What would you expect from someone who had already devised a
        single-function solution? They're probably just ratty about
        people who's solutions aren't just like their own.

        --
        What? Hedgehog
        Notmuchhere: http://www.electrichedgehog.net/

        Comment

        • Kenneth Brody

          #64
          Re: interesting C program

          Chris Dollin wrote:
          >
          Richard Heathfield wrote:
          >
          Chris Dollin said:

          <snip>
          `print` is recursive.
          That's hardly Peter's fault. He's just calling a standard library
          function.
          >
          I have no problem with that. But it makes `print` recursive,
          and hence disallowed by the original specification.
          Must print() call qsort() to itself? Couldn't it call a different
          function?

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

          static int mymax; /* Specs didn't say "no global variables" */

          int print(const void *a, const void *b) {
          static int current = 0;
          if (current < mymax) printf("%d\n", ++current);
          return 0;
          }

          void doit(int i) {
          void *ptr = malloc(i+1); /* Should have test for failure */
          mymax = i;
          qsort(ptr,mymax +1,1,print);
          free(ptr);
          }

          int main(int argc, char *argv[]) {
          int val;
          sscanf(argv[1], "%d", &val);
          doit(val);
          return 0;
          }


          --
          +-------------------------+--------------------+-----------------------+
          | Kenneth J. Brody | www.hvcomputer.com | #include |
          | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
          +-------------------------+--------------------+-----------------------+
          Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


          Comment

          • Benoit Lefebvre

            #65
            Re: interesting C program

            Why not something more simple ?

            Like a sub calling itself.

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

            void printchar(char *mychar, int number)
            {
            int newnumb;
            printf("%s\n", mychar);
            if (number 1) {
            newnumb = number - 1;
            printchar(mycha r, newnumb);
            }
            }

            int main(void)
            {
            printchar("a",1 0); // Print character "a", 10 times! ("a" can be a
            character string too eg: "hello world")
            return 0;
            }
            ------------------------------------

            Comment

            • James Kuyper

              #66
              Re: interesting C program

              Benoit Lefebvre wrote:
              Why not something more simple ?
              >
              Like a sub calling itself.
              Because recursion was one of the many things that were prohibited.

              Comment

              • RoS

                #67
                Re: interesting C program

                In data Wed, 21 Nov 2007 09:27:16 +0100, RoS scrisse:
                >In data Tue, 20 Nov 2007 06:55:08 -0800 (PST), prady scrisse:
                >>hi all,
                >>
                >>could any one solve the following C program. If any one knows the
                >>answer please post it
                >>
                >>Ques:
                >>
                >>A C function that will print 1 to N one per each line on the
                >>stdout , where N is a int parameter to the function. The function
                >>should not
                >>use while, for, do-while loops, goto statement, recursion, and switch
                >>statement.
                why not "add" ifs? :)
                >this is not portable goes well here but for your all computer could
                >cause possibly the format of your hard disk
                #include <stdio.h>

                unsigned i=1;
                char *w=0;
                unsigned ww=0;

                /*
                0ra, 4j
                label:
                */
                void fun0(unsigned j)
                {unsigned *k=&j;
                k-=1;
                ww=*(unsigned*) k;
                }

                int fun3(unsigned j)
                {unsigned *a=&j;
                printf("%u\n", i); ++i;
                i!=j+1 && (a-=1)!=0 && (*(unsigned*)a= ww)!=0;
                return 0;
                }


                int main(void)
                {fun0(0);
                fun3(45);
                return 0;
                }


                Comment

                Working...