[C] A Few Challenging Questions

Discussion in 'Web Design & Coding' started by Moonwraith, May 17, 2008.

  1. Moonwraith

    Moonwraith Moderator Folding Team

    Messages:
    1,131
    Location:
    UK
    A friend of mine stumbled across the following site:

    http://www.gowrikumar.com/programming/index.html

    It's basically made up of questions that are meant to get you into the right state of mind for an interview, apparently. Although, most of them so far have just confused the sweet jesus out of me.

    There's one that's been baffling us for a good few hours now...

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

    If anyone can think of a decent answer to this, then you're a lot better than me (not hard).

    Quite a few fun questions on there mind you. Ideally one day I'm thinking of working through all of them. Couple of other ones currently sucking are: question 3 and figuring out all possible ways to solve 8, currently know of two, but apparently there's at least three.

    Anyone with some novel solutions, chuck a reply please!
     
  2. JPRuss

    JPRuss OSNN Addict Folding Team

    Messages:
    77
    Location:
    In the unknown
    How about:

    Code:
    int onepern (int n)
    {
    fprintf (stdout,"1 to %i one per each line\n",n);
    return 0;
    }
     
    Moonwraith likes this.
  3. BouncingSoul

    BouncingSoul Stranger Than Fiction Political User

    Messages:
    1,297
    Location:
    Sioux Falls, SD
    C is a tool of the devil.
     
  4. JPRuss

    JPRuss OSNN Addict Folding Team

    Messages:
    77
    Location:
    In the unknown
    A better solution?

    Create a really long string that contians all the numbers from 0 - MAXINT (32567) seperated by newlines (\n)

    char *INTNUMBRS = "1\n2\n3\n4\n5 ... \n32567\n"

    Create an array from 0-MAXINT. The values in each array being the chracter position where the Nth number is

    eg:

    INTARRAY[0] = 1;
    INTARRAY[1] = 3;
    INTARRAY[1] = 5;

    etc

    Find the Nth + 1 chracter in the INTARRAY, and just before that position insert the null
    terminatator \0

    NPos = INTARAY[N + 1];
    INTNUMBERS[NPos - 1] = '\0';

    print out INTNUMBERS to stdout.

    Optionally, instead of using the array, you could probably come up with a match function
    that found the Nth chracter in the string. I'm just too tired to do the math for it.
     
  5. Moonwraith

    Moonwraith Moderator Folding Team

    Messages:
    1,131
    Location:
    UK
    If it's a trick question, and I got that in an interview, I'd be totally pissed off :p

    Wonder if there _is_ an elegant solution out there
     
  6. JPRuss

    JPRuss OSNN Addict Folding Team

    Messages:
    77
    Location:
    In the unknown
    Why would you say it is a "trick" question? I don't see any trick to it?