Velocity Reviews

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)

prady 11-20-2007 02:55 PM

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

--
prady

santosh 11-20-2007 03:03 PM

Re: interesting C program
 
In article
<ca717424-770c-44bd-96e1-cc6b6ecf3c0b@b40g2000prf.googlegroups.com>,
prady <prady184u@gmail.com> wrote on Tuesday 20 Nov 2007 8:25 pm:

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


Mark Bluemel 11-20-2007 03:03 PM

Re: interesting C program
 
Previous on Buffy the Vampire Slayer, oops I meant,
comp.please.do.my.homework prady wrote:

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

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,
> comp.please.do.my.homework prady wrote:
>
>> 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.


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
 
In article <fhutv0$f2c$1@tadcaster.hpl.hp.com>,
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:

> In article <fhutv0$f2c$1@tadcaster.hpl.hp.com>,
> 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.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.