interesting C program

Discussion in 'C Programming' started by prady, Nov 20, 2007.

  1. prady

    prady Guest

    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.

    --
    prady
    prady, Nov 20, 2007
    #1
    1. Advertising

  2. prady

    santosh Guest

    In article
    <>,
    prady <> wrote on Tuesday 20 Nov 2007 8:25 pm:

    > hi all,
    >
    > could any one solve the following C program. If any one knows the
    > answer please post it


    This is not a homework group.

    <snip absurd exercise>
    santosh, Nov 20, 2007
    #2
    1. Advertising

  3. prady

    Mark Bluemel Guest

    Previous on Buffy the Vampire Slayer, oops I meant,
    comp.please.do.my.homework prady wrote:

    > 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.


    This was discussed a while back, I personally can't see the point of the
    question. I expect some perverted mind will come up with a solution, but
    I wouldn't be surprised to find their solution flawed/non-portable.
    Mark Bluemel, Nov 20, 2007
    #3
  4. prady

    daya Guest

    int PRINT (int N)
    {
    if(N>0)
    PRINT (N-1);
    printf("%d\n",N);
    return 0;
    }
    daya, Nov 20, 2007
    #4
  5. prady

    Chris Dollin Guest

    Mark Bluemel wrote:

    > Previous on Buffy the Vampire Slayer, oops I meant,
    > comp.please.do.my.homework prady wrote:
    >
    >> 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.

    >
    > This was discussed a while back, I personally can't see the point of the
    > question.


    To make the student thing & explore the possibilities ...

    > I expect some perverted mind will come up with a solution,


    Thank you, kind sir.

    > but I wouldn't be surprised to find their solution flawed/non-portable.


    Actually I believe it's portable and completely straightforward. Also
    an utterly stupid way of printing 1-to-N lines, but that's a different
    issue.

    Note that I'm not saying what the solution /is/, since I too believe
    that Prady will get more benefit from working out the solution themself
    than from having one of us provide it.

    Here's a clue: it requires functions from C's standard library.

    --
    Chris "cosh - no. Kosh - 'you do not understand, but you will.'" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Nov 20, 2007
    #5
  6. prady

    Chris Dollin Guest

    daya wrote:

    > int PRINT (int N)
    > {
    > if(N>0)
    > PRINT (N-1);
    > printf("%d\n",N);
    > return 0;
    > }


    What part of "The function should not use while, for, do-while loops,
    goto statement, /recursion/, and switch statement" [emphasis mine]
    don't you understand?

    [Why is `PRINT` spelled with CAPS when it's not a MACRO?]

    --
    Chris "my HAT isn't a MACRO either" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Nov 20, 2007
    #6
  7. In article <fhutv0$f2c$>,
    Chris Dollin <> wrote:

    >>> 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.


    [...]

    >Actually I believe it's portable and completely straightforward. Also
    >an utterly stupid way of printing 1-to-N lines, but that's a different
    >issue.


    Can you see any point to the prohibition of switch statements?

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Nov 20, 2007
    #7
  8. prady

    Rachael Guest

    There's the silly solution:

    int i=1;
    /* and then write out INT_MAX times: */
    if (i<=N) {
    printf("%d\n",i);
    i++;
    }

    I once failed an interview question very similar to this, because I
    couldn't imagine they were looking for such an inelegant "solution",
    but they were.
    Rachael, Nov 20, 2007
    #8
  9. prady

    Richard Guest

    (Richard Tobin) writes:

    > In article <fhutv0$f2c$>,
    > Chris Dollin <> wrote:
    >
    >>>> 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.

    >
    > [...]
    >
    >>Actually I believe it's portable and completely straightforward. Also
    >>an utterly stupid way of printing 1-to-N lines, but that's a different
    >>issue.

    >
    > Can you see any point to the prohibition of switch statements?
    >
    > -- Richard


    To encourage students to think "out of the box" pretty much like any
    teaching course. Most exercises given on courses have almost no
    practical usage in their own right. The idea is to stimulate the student
    into thinking about the language and it's features.
    Richard, Nov 20, 2007
    #9
  10. prady

    Richard Guest

    Rachael <> writes:

    > There's the silly solution:
    >
    > int i=1;
    > /* and then write out INT_MAX times: */
    > if (i<=N) {
    > printf("%d\n",i);
    > i++;
    > }
    >
    > I once failed an interview question very similar to this, because I
    > couldn't imagine they were looking for such an inelegant "solution",
    > but they were.


    I'd love to see some of the questions that a few of the core members of
    this group would set the hapless interviewees :-;
    Richard, Nov 20, 2007
    #10
  11. prady

    Ben Pfaff Guest

    Rachael <> writes:

    > There's the silly solution:
    >
    > int i=1;
    > /* and then write out INT_MAX times: */
    > if (i<=N) {
    > printf("%d\n",i);
    > i++;
    > }


    Why use a variable then?

    if (i <= 1)
    puts("1");
    if (i <= 2)
    puts("2");
    if (i <= 3)
    puts("3");
    ....
    --
    "If I've told you once, I've told you LLONG_MAX times not to
    exaggerate."
    --Jack Klein
    Ben Pfaff, Nov 20, 2007
    #11
  12. prady

    Chris Dollin Guest

    Richard Tobin wrote:

    > In article <fhutv0$f2c$>,
    > Chris Dollin <> wrote:
    >
    >>>> 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.

    >
    > [...]
    >
    >>Actually I believe it's portable and completely straightforward. Also
    >>an utterly stupid way of printing 1-to-N lines, but that's a different
    >>issue.

    >
    > Can you see any point to the prohibition of switch statements?


    It might be to prevent the student from wasting their time thinking of a
    way of using them; otherwise, no.

    [I've now managed to turn my insight into code. Perhaps "completely
    straightforward" was a little optimistic, eg there's an `if` in there
    whose condition is always true ... I think there's room for improvement.]

    --
    Chris "and another one which isn't" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Nov 20, 2007
    #12
  13. prady

    Ben Pfaff Guest

    prady <> writes:

    > 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.


    Need it be portable?

    void function(int n)
    {
    char cmd[64];
    sprintf(cmd, "seq %d", n);
    system(cmd);
    }
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Nov 20, 2007
    #13
  14. prady

    Chris Dollin Guest

    Chris Dollin wrote:

    > [I've now managed to turn my insight into code. Perhaps "completely
    > straightforward" was a little optimistic, eg there's an `if` in there
    > whose condition is always true ... I think there's room for improvement.]


    There was.

    Shiny! That fixed the 0 case, too!

    --
    Chris "money for old rope" Dollin

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, Nov 20, 2007
    #14
  15. prady

    Richard Guest

    Ben Pfaff <> writes:

    > Rachael <> writes:
    >
    >> There's the silly solution:
    >>
    >> int i=1;
    >> /* and then write out INT_MAX times: */
    >> if (i<=N) {
    >> printf("%d\n",i);
    >> i++;
    >> }

    >
    > Why use a variable then?
    >
    > if (i <= 1)
    > puts("1");
    > if (i <= 2)
    > puts("2");
    > if (i <= 3)
    > puts("3");
    > ...


    Because for a program which covers all integers from 0 to MAX_INT N is
    the cut off.
    Richard, Nov 20, 2007
    #15
  16. prady

    Rachael Guest

    On 20 Nov, 15:55, Ben Pfaff <> wrote:

    > Why use a variable then?
    >
    > if (i <= 1)
    > puts("1");
    > if (i <= 2)
    > puts("2");
    > if (i <= 3)
    > puts("3");
    > ...


    Assuming you mean
    if (N >= 1)
    then yes, you're right.
    Rachael, Nov 20, 2007
    #16
  17. Ben Pfaff wrote:
    > prady <> writes:
    >
    >> 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.

    >
    > Need it be portable?
    >
    > void function(int n)
    > {
    > char cmd[64];
    > sprintf(cmd, "seq %d", n);
    > system(cmd);
    > }


    If you're not worried about portability, then look at my program:

    void _G(int n);

    void f(int n) {
    _G(n);
    }

    Naturally, _G() is to be provided by the implementation.
    Philip Potter, Nov 20, 2007
    #17
  18. On Tue, 20 Nov 2007 06:55:08 -0800, prady wrote:

    > 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.

    Hint: look up the standard library functions setjmp and longjmp.
    Duncan Muirhead, Nov 20, 2007
    #18
  19. Rachael schrieb:
    > There's the silly solution:

    [Snip]

    That's what I thought first, but couldn't really believe they were
    asking for that.

    > I once failed an interview question very similar to this, because I
    > couldn't imagine they were looking for such an inelegant "solution",
    > but they were.


    Most braindead assignment *ever*, seriously. Whoever asks these
    questions should not be allowed to teach/have a leading position in HR.
    BTW, did you get the job anyways?

    Greetings,
    Johannes

    --
    "Viele der Theorien der Mathematiker sind falsch und klar
    Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathematik <4740ad67$0$3811$>
    Johannes Bauer, Nov 20, 2007
    #19
  20. prady

    Rachael Guest

    On 20 Nov, 17:21, Johannes Bauer <> wrote:

    > Most braindead assignment *ever*, seriously. Whoever asks these
    > questions should not be allowed to teach/have a leading position in HR.
    > BTW, did you get the job anyways?


    The other questions in the interview were very interesting. I think
    with that question they wanted to see whether I was blind to the
    "braindead" solution when there were no other options.
    I didn't get the job, but it's OK, because it was about an hour away
    by train and my current job is 15 minutes away by bike :)
    Rachael, Nov 20, 2007
    #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. Tim Hubberstey

    "Interesting" behavior with aggregates

    Tim Hubberstey, Jun 25, 2004, in forum: VHDL
    Replies:
    6
    Views:
    1,821
    Tim Hubberstey
    Jun 30, 2004
  2. Simon Harvey
    Replies:
    5
    Views:
    437
    Scott M.
    Nov 16, 2003
  3. Jay
    Replies:
    1
    Views:
    360
    Chris Taylor
    Oct 2, 2003
  4. Elliot M. Rodriguez

    An interesting problem

    Elliot M. Rodriguez, Oct 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    390
    Elliot M. Rodriguez
    Oct 21, 2003
  5. Replies:
    2
    Views:
    1,011
    Nick Keighley
    May 6, 2008
Loading...

Share This Page