interesting C program

P

prady

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

santosh

prady said:
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>
 
M

Mark Bluemel

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

Chris Dollin

Mark said:
Previous on Buffy the Vampire Slayer, oops I meant,


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

Chris Dollin

daya said:
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?]
 
R

Richard Tobin

[...]

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.[/QUOTE]

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

-- Richard
 
R

Rachael

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

Richard

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[/QUOTE]

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

Richard

Rachael said:
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 :-;
 
B

Ben Pfaff

Rachael said:
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");
....
 
C

Chris Dollin

Richard said:
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?[/QUOTE]

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

Ben Pfaff

prady said:
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);
}
 
C

Chris Dollin

Chris said:
[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!
 
R

Richard

Ben Pfaff said:
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.
 
P

Philip Potter

Ben said:
prady said:
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.
 
D

Duncan Muirhead

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

Johannes Bauer

Rachael said:
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
 
R

Rachael

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top