Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   interesting C program (http://www.velocityreviews.com/forums/t553815-interesting-c-program.html)

interesting C program

hi all,

could any one solve the following C program. If any one knows the

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.

--

 santosh 11-20-2007 03:03 PM

Re: interesting C program

In article

> hi all,
>
> could any one solve the following C program. If any one knows the

This is not a homework group.

<snip absurd exercise>

 Mark Bluemel 11-20-2007 03:03 PM

Re: interesting C program

Previous on Buffy the Vampire Slayer, oops I meant,

> hi all,
>
> could any one solve the following C program. If any one knows the
>
> 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.

 daya 11-20-2007 03:19 PM

Re: interesting C program

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

 Chris Dollin 11-20-2007 03:21 PM

Re: interesting C program

Mark Bluemel wrote:

> Previous on Buffy the Vampire Slayer, oops I meant,
>
>> hi all,
>>
>> could any one solve the following C program. If any one knows the
>>
>> 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 11-20-2007 03:24 PM

Re: interesting C program

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

 Richard Tobin 11-20-2007 03:38 PM

Re: interesting C program

Chris Dollin <chris.dollin@hp.com> 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.

 Rachael 11-20-2007 03:42 PM

Re: interesting C program

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.

 Richard 11-20-2007 03:42 PM

Re: interesting C program

richard@cogsci.ed.ac.uk (Richard Tobin) writes:

> Chris Dollin <chris.dollin@hp.com> 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 11-20-2007 03:53 PM

Re: interesting C program

Rachael <rachaelluckett@gmail.com> 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 :-;

All times are GMT. The time now is 02:37 PM.