• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[C] A Few Challenging Questions

#1
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
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;
}
 
#4
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.
 

Members online

No members online now.

Latest posts

Latest profile posts

Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,014
Latest member
sanoravies