[C] A Few Challenging Questions

Moonwraith

OSNN Veteran Addict
Joined
22 Oct 2004
Messages
1,133
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!
 

JPRuss

OSNN Addict
Joined
5 May 2008
Messages
77
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.
How about:

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

JPRuss

OSNN Addict
Joined
5 May 2008
Messages
77
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.
 

Moonwraith

OSNN Veteran Addict
Joined
22 Oct 2004
Messages
1,133
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
 

JPRuss

OSNN Addict
Joined
5 May 2008
Messages
77
Why would you say it is a "trick" question? I don't see any trick to it?
 

Members online

No members online now.

Latest profile posts

hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.
Terrahertz wrote on Electronic Punk's profile.
Yo fellas!
Electronic Punk wrote on Sazar's profile.
Where are you buddy?
Perris Calderon wrote on Electronic Punk's profile.
Hey EP! All good with me, applying for Microsoft MVP right now, should have done this a while ago.

Notifications don't work, I only found your response by coming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier!

Forum statistics

Threads
61,999
Messages
673,424
Members
5,593
Latest member
moussa021