468,463 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

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
Nov 20 '07 #1
66 3306
In article
<ca**********************************@b40g2000prf. googlegroups.com>,
prady <pr*******@gmail.comwrote 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>

Nov 20 '07 #2
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.
Nov 20 '07 #3
int PRINT (int N)
{
if(N>0)
PRINT (N-1);
printf("%d\n",N);
return 0;
}
Nov 20 '07 #4
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

Nov 20 '07 #5
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

Nov 20 '07 #6
In article <fh**********@tadcaster.hpl.hp.com>,
Chris Dollin <ch**********@hp.comwrote:
>>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.
Nov 20 '07 #7
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.
Nov 20 '07 #8
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <fh**********@tadcaster.hpl.hp.com>,
Chris Dollin <ch**********@hp.comwrote:
>>>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.
Nov 20 '07 #9
Richard Tobin wrote:
In article <fh**********@tadcaster.hpl.hp.com>,
Chris Dollin <ch**********@hp.comwrote:
>>>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?
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.]

--
Chris "and another one which isn't" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Nov 20 '07 #10
Rachael <ra************@gmail.comwrites:
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");
....
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Nov 20 '07 #11
prady <pr*******@gmail.comwrites:
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);
}
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Nov 20 '07 #12
Rachael <ra************@gmail.comwrites:
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 :-;
Nov 20 '07 #13
Chris Dollin wrote:
[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!

--
Chris "money for old rope" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Nov 20 '07 #14
On 20 Nov, 15:55, Ben Pfaff <b...@cs.stanford.eduwrote:
Why use a variable then?

if (i <= 1)
puts("1");
if (i <= 2)
puts("2");
if (i <= 3)
puts("3");
...
Assuming you mean
if (N >= 1)
then yes, you're right.
Nov 20 '07 #15
Ben Pfaff <bl*@cs.stanford.eduwrites:
Rachael <ra************@gmail.comwrites:
>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");
...
Because for a program which covers all integers from 0 to MAX_INT N is
the cut off.

Nov 20 '07 #16
Ben Pfaff wrote:
prady <pr*******@gmail.comwrites:
>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.
Nov 20 '07 #17
Rachael schrieb:
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

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
Nov 20 '07 #18
On Tue, 20 Nov 2007 06:55:08 -0800, 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.
Hint: look up the standard library functions setjmp and longjmp.
Nov 20 '07 #19
On 20 Nov, 17:21, Johannes Bauer <dfnsonfsdu...@gmx.dewrote:
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 :)
Nov 20 '07 #20
On Tue, 20 Nov 2007 07:55:55 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
>Rachael <ra************@gmail.comwrites:
>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?
So you can cut and paste :-)
>if (i <= 1)
puts("1");
if (i <= 2)
puts("2");
if (i <= 3)
puts("3");
...
--
Al Balmer
Sun City, AZ
Nov 20 '07 #21
prady wrote On 11/20/07 09:55,:
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.
Here you go. Note that this version only works for
`int' values up to the guaranteed minimum of 32767; if
your implementation supports larger `int' values, make
the obvious modifications.

typedef void(o)(int,int),O;O theFunction(int N){o OO;OO(00,N
);}O Oo(int poq,int puq){o OoQ;OoQ(puq,poq);OoQ(++puq,poq);}
O oO(int qup,int qpu){Oo(qpu,qup++);Oo(qpu,++qup);}O o0(int\
pqu,int ppq){ oO(ppq,pqu);oO(ppq+04,pqu);}O O0(int puq,int\
poq){o0(poq,puq);o0 (poq,puq+010);}O oo(int qop,int qpp){O0
(qpp,qop);O0(qpp+16,qop);}O QO(int ppq,int oqu){oo(oqu,ppq);
oo(oqu,ppq+32);}O Qo(int poq,int puq){QO(puq,poq);QO(puq+01\
00,poq);}O QQ(int qup,int qpu){Qo(qpu,qup );Qo(qpu,qup+128);
}O Q0(int pqu,int puq){QQ(puq,pqu);QQ(puq+256,pqu );}O OQ(i\
nt uqp,int poq){Q0(poq,uqp);Q0(poq,uqp+512);}O oQ(int qop, \
int puq){OQ(puq,qop);OQ(puq+1024,qop);}O oOo(int puq,int poq
){oQ(poq ,puq);oQ(poq,puq+2048);}O OoO(int qpu,int qup){oOo(
qup,qpu);oOo(qup+ 4096,qpu);}O OO(int ppq,int pqu){OoO(pqu,\
ppq);OoO(pqu,ppq+16384);}
#include <stdio.h>
O OoQ(int qpp,int poq){if(qpp&!(qpp>poq))printf("%d\n",qpp);}

--
Er*********@sun.com
Nov 20 '07 #22
Eric Sosman wrote On 11/20/07 15:16,:
prady wrote On 11/20/07 09:55,:
>>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.


Here you go. Note that this version only works for
`int' values up to the guaranteed minimum of 32767; if
your implementation supports larger `int' values, make
the obvious modifications.

typedef void(o)(int,int),O;O theFunction(int N){o OO;OO(00,N
);}O Oo(int poq,int puq){o OoQ;OoQ(puq,poq);OoQ(++puq,poq);}
O oO(int qup,int qpu){Oo(qpu,qup++);Oo(qpu,++qup);}O o0(int\
pqu,int ppq){ oO(ppq,pqu);oO(ppq+04,pqu);}O O0(int puq,int\
poq){o0(poq,puq);o0 (poq,puq+010);}O oo(int qop,int qpp){O0
(qpp,qop);O0(qpp+16,qop);}O QO(int ppq,int oqu){oo(oqu,ppq);
oo(oqu,ppq+32);}O Qo(int poq,int puq){QO(puq,poq);QO(puq+01\
00,poq);}O QQ(int qup,int qpu){Qo(qpu,qup );Qo(qpu,qup+128);
}O Q0(int pqu,int puq){QQ(puq,pqu);QQ(puq+256,pqu );}O OQ(i\
nt uqp,int poq){Q0(poq,uqp);Q0(poq,uqp+512);}O oQ(int qop, \
int puq){OQ(puq,qop);OQ(puq+1024,qop);}O oOo(int puq,int poq
){oQ(poq ,puq);oQ(poq,puq+2048);}O OoO(int qpu,int qup){oOo(
qup,qpu);oOo(qup+ 4096,qpu);}O OO(int ppq,int pqu){OoO(pqu,\
ppq);OoO(pqu,ppq+16384);}
#include <stdio.h>
O OoQ(int qpp,int poq){if(qpp&!(qpp>poq))printf("%d\n",qpp);}
Oh, drat! Silly typo: Fix the bug by changing `&'
to `&&', of course.

--
Er*********@sun.com

Nov 20 '07 #23
On Nov 20, 3:21 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
Eric Sosman wrote On 11/20/07 15:16,:
prady wrote On 11/20/07 09:55,:
>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.
Here you go. Note that this version only works for
`int' values up to the guaranteed minimum of 32767; if
your implementation supports larger `int' values, make
the obvious modifications.
typedef void(o)(int,int),O;O theFunction(int N){o OO;OO(00,N
);}O Oo(int poq,int puq){o OoQ;OoQ(puq,poq);OoQ(++puq,poq);}
O oO(int qup,int qpu){Oo(qpu,qup++);Oo(qpu,++qup);}O o0(int\
pqu,int ppq){ oO(ppq,pqu);oO(ppq+04,pqu);}O O0(int puq,int\
poq){o0(poq,puq);o0 (poq,puq+010);}O oo(int qop,int qpp){O0
(qpp,qop);O0(qpp+16,qop);}O QO(int ppq,int oqu){oo(oqu,ppq);
oo(oqu,ppq+32);}O Qo(int poq,int puq){QO(puq,poq);QO(puq+01\
00,poq);}O QQ(int qup,int qpu){Qo(qpu,qup );Qo(qpu,qup+128);
}O Q0(int pqu,int puq){QQ(puq,pqu);QQ(puq+256,pqu );}O OQ(i\
nt uqp,int poq){Q0(poq,uqp);Q0(poq,uqp+512);}O oQ(int qop, \
int puq){OQ(puq,qop);OQ(puq+1024,qop);}O oOo(int puq,int poq
){oQ(poq ,puq);oQ(poq,puq+2048);}O OoO(int qpu,int qup){oOo(
qup,qpu);oOo(qup+ 4096,qpu);}O OO(int ppq,int pqu){OoO(pqu,\
ppq);OoO(pqu,ppq+16384);}
#include <stdio.h>
O OoQ(int qpp,int poq){if(qpp&!(qpp>poq))printf("%d\n",qpp);}

Oh, drat! Silly typo: Fix the bug by changing `&'
to `&&', of course.
Of course.
Nov 20 '07 #24
On Tue, 20 Nov 2007 15:21:43 -0500, Eric Sosman wrote:
Eric Sosman wrote On 11/20/07 15:16,:
>prady wrote On 11/20/07 09:55,:
>>>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.


Here you go. Note that this version only works for
`int' values up to the guaranteed minimum of 32767; if your
implementation supports larger `int' values, make the obvious
modifications.

typedef void(o)(int,int),O;O theFunction(int N){o OO;OO(00,N
);}O Oo(int poq,int puq){o OoQ;OoQ(puq,poq);OoQ(++puq,poq);}
O oO(int qup,int qpu){Oo(qpu,qup++);Oo(qpu,++qup);}O o0(int\
pqu,int ppq){ oO(ppq,pqu);oO(ppq+04,pqu);}O O0(int puq,int\
poq){o0(poq,puq);o0 (poq,puq+010);}O oo(int qop,int qpp){O0
(qpp,qop);O0(qpp+16,qop);}O QO(int ppq,int oqu){oo(oqu,ppq);
oo(oqu,ppq+32);}O Qo(int poq,int puq){QO(puq,poq);QO(puq+01\
00,poq);}O QQ(int qup,int qpu){Qo(qpu,qup );Qo(qpu,qup+128);
}O Q0(int pqu,int puq){QQ(puq,pqu);QQ(puq+256,pqu );}O OQ(i\
nt uqp,int poq){Q0(poq,uqp);Q0(poq,uqp+512);}O oQ(int qop, \
int puq){OQ(puq,qop);OQ(puq+1024,qop);}O oOo(int puq,int poq
){oQ(poq ,puq);oQ(poq,puq+2048);}O OoO(int qpu,int qup){oOo(
qup,qpu);oOo(qup+ 4096,qpu);}O OO(int ppq,int pqu){OoO(pqu,\
ppq);OoO(pqu,ppq+16384);}
#include <stdio.h>
O OoQ(int qpp,int poq){if(qpp&!(qpp>poq))printf("%d\n",qpp);}

Oh, drat! Silly typo: Fix the bug by changing `&'
to `&&', of course.
With that fixed, your function is still missing the numbers from 8192 to
16383.
Nov 20 '07 #25
Eric Sosman wrote:
Eric Sosman wrote On 11/20/07 15:16,:
>prady wrote On 11/20/07 09:55,:
>>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.

Here you go. Note that this version only works for
`int' values up to the guaranteed minimum of 32767; if
your implementation supports larger `int' values, make
the obvious modifications.

typedef void(o)(int,int),O;O theFunction(int N){o OO;OO(00,N
);}O Oo(int poq,int puq){o OoQ;OoQ(puq,poq);OoQ(++puq,poq);}
O oO(int qup,int qpu){Oo(qpu,qup++);Oo(qpu,++qup);}O o0(int\
pqu,int ppq){ oO(ppq,pqu);oO(ppq+04,pqu);}O O0(int puq,int\
poq){o0(poq,puq);o0 (poq,puq+010);}O oo(int qop,int qpp){O0
(qpp,qop);O0(qpp+16,qop);}O QO(int ppq,int oqu){oo(oqu,ppq);
oo(oqu,ppq+32);}O Qo(int poq,int puq){QO(puq,poq);QO(puq+01\
00,poq);}O QQ(int qup,int qpu){Qo(qpu,qup );Qo(qpu,qup+128);
}O Q0(int pqu,int puq){QQ(puq,pqu);QQ(puq+256,pqu );}O OQ(i\
nt uqp,int poq){Q0(poq,uqp);Q0(poq,uqp+512);}O oQ(int qop, \
int puq){OQ(puq,qop);OQ(puq+1024,qop);}O oOo(int puq,int poq
){oQ(poq ,puq);oQ(poq,puq+2048);}O OoO(int qpu,int qup){oOo(
qup,qpu);oOo(qup+ 4096,qpu);}O OO(int ppq,int pqu){OoO(pqu,\
ppq);OoO(pqu,ppq+16384);}
#include <stdio.h>
O OoQ(int qpp,int poq){if(qpp&!(qpp>poq))printf("%d\n",qpp);}

Oh, drat! Silly typo: Fix the bug by changing `&'
to `&&', of course.
I'm reminded of an excerpt from _Learning Perl_:

"Of course, choosing good or poor names makes no difference to Perl. You
could name your program's three most-important variables $OOO000OOO,
$OO00OO00, and $O0O0O0O0O and Perl wouldn't be bothered -- but in that
case, please, don't ask us to maintain your code."

--
SM
rot13 for email
Nov 20 '07 #26
On Nov 20, 6:55 am, prady <prady1...@gmail.comwrote:
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.
Run this program to complete your assignment.
Give it a number N on the command line, then look for test.c:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define q0 26
#define q1 (q0/2)
#define q2 'a'
#define q3 'A'
int q4(int q5)??<int q6=q2;if(isalpha(q5))??<if(isupper(q5))q6=q3;
q5=(q5-q6+q1)%q0+q6;??>return q5;??>char*q7(char*q8)??<int q9;
char*q10=q8;
for(q9=0;*q8;q9++)??<*q8=q4(*q8);q8++;??>return q10;??>int main(int
q11,char**q12)??<FILE*q13;size_t q14;if(q11<2)??<puts(q7("\122\105"
"\105\102\105\72\40\114\142\150\40\156\145\162\40\ 141\142\147\40\156"
"\157\171\162\40\147\142\40\160\145\162\156\14 7\
\162\40\156\40\163\166\171\162\40\156\147\40\147\1 65\166\146\40\147"
"\166\172\162\56"
));exit(EXIT_FAILURE);??>q13=fopen(q7("\147\162\14 6\
\147\56\160"),q7("\152"));if(q13==NULL)??<puts(q7( "\110\106\116\124"
"\122\72\40\163\142\142\40\74\101\76\n\t\152\165\1 62\
\145\162\40\101\40\166\146\40\156\40\141\156\147\1 50\145\156\171\40"
"\166\141\147\162\164\162\145"
));exit(EXIT_FAILURE);??>fputs(q7("\43\166\141\160 \171\150\161\162\
\40\74\146\147\161\166\142\56\165\76\n"),q13);fput s(q7("\43\166\141"
"\160\171\150\161\162\40\74\146\147\161\171\166\15 7\56\165\76\n"
),q13);fputs(q7("\43\166\141\160\171\150\161\162\4 0\74\146\147\145"
"\166\141\164\56\165\76\n"
),q13);fputs(q7("\n"),q13);fputs(q7(
"\166\141\147\40\172\156\166\141\50\151\142\166\16 1\51\n"),q13);
fputs(
q7("\173\n"),q13);for(q14=1;q14<=atoi(q12??(1??)); q14++)fprintf(q13,
"\160\165\164\163\50\"\45\165\"\51\73\n",(unsigned )q14);fputs(q7(
"\145\162\147\150\145\141\40\60\73\n"),q13);fputs( q7("\175\n"),q13);
return 0;??>
/*
Was I a little too literal?
*/
Nov 20 '07 #27
Harald van Dijk wrote On 11/20/07 15:47,:
On Tue, 20 Nov 2007 15:21:43 -0500, Eric Sosman wrote:
>>Eric Sosman wrote On 11/20/07 15:16,:
>>>prady wrote On 11/20/07 09:55,:
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.
Here you go. Note that this version only works for
`int' values up to the guaranteed minimum of 32767; if your
implementation supports larger `int' values, make the obvious
modifications.

typedef void(o)(int,int),O;O theFunction(int N){o OO;OO(00,N
);}O Oo(int poq,int puq){o OoQ;OoQ(puq,poq);OoQ(++puq,poq);}
O oO(int qup,int qpu){Oo(qpu,qup++);Oo(qpu,++qup);}O o0(int\
pqu,int ppq){ oO(ppq,pqu);oO(ppq+04,pqu);}O O0(int puq,int\
poq){o0(poq,puq);o0 (poq,puq+010);}O oo(int qop,int qpp){O0
(qpp,qop);O0(qpp+16,qop);}O QO(int ppq,int oqu){oo(oqu,ppq);
oo(oqu,ppq+32);}O Qo(int poq,int puq){QO(puq,poq);QO(puq+01\
00,poq);}O QQ(int qup,int qpu){Qo(qpu,qup );Qo(qpu,qup+128);
}O Q0(int pqu,int puq){QQ(puq,pqu);QQ(puq+256,pqu );}O OQ(i\
nt uqp,int poq){Q0(poq,uqp);Q0(poq,uqp+512);}O oQ(int qop, \
int puq){OQ(puq,qop);OQ(puq+1024,qop);}O oOo(int puq,int poq
){oQ(poq ,puq);oQ(poq,puq+2048);}O OoO(int qpu,int qup){oOo(
qup,qpu);oOo(qup+ 4096,qpu);}O OO(int ppq,int pqu){OoO(pqu,\
ppq);OoO(pqu,ppq+16384);}
#include <stdio.h>
O OoQ(int qpp,int poq){if(qpp&!(qpp>poq))printf("%d\n",qpp);}

Oh, drat! Silly typo: Fix the bug by changing `&'
to `&&', of course.

With that fixed, your function is still missing the numbers from 8192 to
16383.
Oh, double drat! Triple drat, in fact, because it's also
missing 24576 through 32767. Well, the symptom pinpoints the
error pretty clearly, and the O.P. will benefit from debugging
my clumsiness.

The phrase "Too clever by a power of two" comes to mind.

--
Er*********@sun.com

Nov 20 '07 #28
In article <rs************@news.individual.net>,
Richard <rg****@gmail.comwrote:
>>>>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.
>Can you see any point to the prohibition of switch statements?
>To encourage students to think "out of the box" pretty much like any
teaching course.
But what on earth would you use a switch statement for? Note that
"if" is not prohibited.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 20 '07 #29
In article <ca**********************************@b40g2000prf. googlegroups.com>,
prady <pr*******@gmail.comwrote:
>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.
Since someone has now mentioned the "obvious" solution, let's see
how you might arrive at it.

First let's discard the solutions which require, one way or another,
a program whose size depends on the maximum value of N. The simplest
one is:

void f(int N)
{
if(N < 1)
return;
printf("1\n");
if(N < 2)
return;
printf("2\n");
...
}

Much more elegant is Eric Sosman's solution, but it has the same problem.

It should be clear that to handle an arbitrarily large value
(obviously any implementation will have a limit, but the program need
not depend on it) we will need to execute the same code repeatedly, an
unbounded number of times. The problem prohibits all the relevant
syntactic control constructs: looping and branching, and recursion
(which is the usual answer to questions of this kind).

That leaves non-syntactic control constructs, of which there is only
one: setjmp()/longjmp(). And presumably that is the expected answer.

One conclusion you might draw from this is that setjmp()/longjmp() is
a bizarre aberration in the design of C. There should obviously be a
built-in syntactic construct instead of a pair of library functions.
This is even clearer when you read the peculiar restrictions on where
setjmp() can be used.

Finally, here is a solution:

#include <stdio.h>
#include <setjmp.h>

void f(int n);

int main(int argc, char **argv)
{
int n;

n = atoi(argv[1]); /* error handling omitted */
f(n);

return 0;
}

void f(int n)
{
volatile int i=1;
jmp_buf env;

setjmp(env);

printf("%d\n", i);
if(i >= n)
return;
i++;

longjmp(env, 0);
}

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 20 '07 #30
Richard Tobin schrieb:
That leaves non-syntactic control constructs, of which there is only
one: setjmp()/longjmp(). And presumably that is the expected answer.
That actually is the point why I heavily dislike the
setjmp()/longjmp()-version: because they *expect* you to come up with
it. Some teacher comes up with an incredibly bizzare assignment, makes
ridiculous restrictions on what constructs are okay and which aren't and
then still tries to force you to think the way he does.

The other solutions (I especially like Eric's) much more than the
setjmp()-version demonstrate that people are able to think out of the
box. And they impressively demonstrate that when you ask a really stupid
question (like this assignment) you get a really stupid answer
(beautiful yet totally obfuscated code).

If any teacher in the world would dare to give no points to a solution
like Eric's, I'd like to tear him to pieces. But I very much believe
there are many of them. Self-declared C-"programmers" who think their
solutions are bleeding edge. I'm so sick of them.

Probably because I was confronted with a lot of these people at college.
Horrible, horrible experiences coming up. Yeah well, now I shared them ;-)

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
Nov 20 '07 #31
Johannes Bauer said:
Richard Tobin schrieb:
>That leaves non-syntactic control constructs, of which there is only
one: setjmp()/longjmp(). And presumably that is the expected answer.
Certainly that's the answer I thought of - and that's why I didn't even
bother to code it up privately. I *hate* setjmp/longjmp with a deep and
abiding passion. There is a special hell reserved for those who introduce
these functions into any C program.
That actually is the point why I heavily dislike the
setjmp()/longjmp()-version: because they *expect* you to come up with
it. Some teacher comes up with an incredibly bizzare assignment, makes
ridiculous restrictions on what constructs are okay and which aren't and
then still tries to force you to think the way he does.
But it does suggest an interesting idea - that someone who is beyond
suspicion of "do my homework" could come up with a similar, ludicrously
restricted problem *for which they must have at least one solution in
mind*, just to see what kind of solutions people manage to come up with.

It would, at least, make for a diverting exercise!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 20 '07 #32
[comp.lang.c] Al Balmer <al******@att.netwrote:
On Tue, 20 Nov 2007 07:55:55 -0800, Ben Pfaff <bl*@cs.stanford.edu>
>>Why use a variable then?
So you can cut and paste :-)
Alas, I'm not aware of a pen that does that :-)

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Nov 21 '07 #33
Johannes Bauer wrote:
[...]
If any teacher in the world would dare to give no points to a solution
like Eric's, I'd like to tear him to pieces.
So would I, especially since I blew it. Twice. :-(
But I very much believe
there are many of them. Self-declared C-"programmers" who think their
solutions are bleeding edge. I'm so sick of them.

Probably because I was confronted with a lot of these people at college.
Horrible, horrible experiences coming up. Yeah well, now I shared them ;-)
There exist those who confound ability with cleverness, or
even with "Aha! I've seen *that* one before!"-ness. Some people
ask trick questions whose answers you won't know unless you've
already seen the trick or unless you're truly twisted. Nobody
but nobody asked questions based on Duff's Device until Duff
devised it; what cleverness do they test for (or demonstrate!)
by checking whether someone has or hasn't run across this or
that bizarre-but-legal construct? To Duff the glory; to the
Devil with the camp followers!

There's a scene in Wagner's "Siegfried" in which the god
Wotan agrees to be questioned by the dwarf Mime if Mime will
in turn submit to Wotan's questions. Mime quizzes Wotan, and
Wotan scores a hundred percent. Wotan then puts posers to
Mime, and Mime flubs them all. At the end, Wotan remarks on
Mime's fundamental mistake: Given the opportunity to learn from
a god, he has wasted it by asking questions whose answers he
already knew instead of questions whose answers might inform
him. That's a clue about how I regard the aren't-I-clever-for-
having-read-about-this-gadget-before-you-did examiners.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 21 '07 #34
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.
Recursion is disallowed, but a function can call a separate but
otherwise identical function. With a divide and conquer approach, you
can get away with one function per bit. Each function calls the next
one twice. For example...

void print1(int low, int high, int x) {
print2(low, high/2, x);
print2(high/2, high, x);
}

int main(void) {
print1(0, INT_MAX, input_value);
}

print2 is identical to print1, except it calls print3(). And so on -
and the last function does the printing. Only 32 functions needed for a
32 bit int - not bad.

We can optimize this a bit (see below).

But we can do even better than 32 functions. We can wrap up the
definition of this print family of functions in a macro to define them
easily, and then we can apply the exact same divide and conquer trick to
the macro itself! We need to output 32 functions, and we can do that
with one macro per bit - so we only need 5 macros if we play our cards
right.

And, of course, we need the very silly function to do the actual printing.

Complete code follows. I submit this is better than the longjmp()
solution and I encourage all prospective interviewees to use this instead.
#include <stdio.h>
#include <limits.h>

void print_nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn(int x, unsigned low,
unsigned high) {
if (high 0 && x >= high) printf("%d\n", high);
}

#define MAKE_PRINT(fun)\
void print_ ## fun (int x, unsigned low, unsigned high) {\
if (x < low) return; \
print_ ##fun##n (x, low, low + (high - low)/2);\
print_ ##fun##n (x, low + (high - low)/2, high);\
}

#define MAKE_PRINT_2(q) \
MAKE_PRINT(q ## n) MAKE_PRINT(q)
#define MAKE_PRINT_3(q) \
MAKE_PRINT_2(q ## nn) MAKE_PRINT_2(q)
#define MAKE_PRINT_4(q) \
MAKE_PRINT_3(q ## nnnn) MAKE_PRINT_3(q)
#define MAKE_PRINT_5(q) \
MAKE_PRINT_4(q ## nnnnnnnn) MAKE_PRINT_4(q)
#define MAKE_PRINT_6(q) \
MAKE_PRINT_5(q ## nnnnnnnnnnnnnnnn) MAKE_PRINT_5(q)

MAKE_PRINT_6()

int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
print_(val, 0, UINT_MAX);
return 0;
}
Nov 21 '07 #35
Peter Ammon wrote:
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.

Recursion is disallowed, but a function can call a separate but
otherwise identical function.
Thereby failing the requirement "/A/ C function" [emphasis mine].
Just the one, Mrs Wembley.

--
On The Up Hedgehog
Otherface: Jena RDF/Owl toolkit http://jena.sourceforge.net/

Nov 21 '07 #36
Chris Dollin wrote:
Peter Ammon wrote:
>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.
Recursion is disallowed, but a function can call a separate but
otherwise identical function.

Thereby failing the requirement "/A/ C function" [emphasis mine].
Just the one, Mrs Wembley.
I'm pretty sure you are permitted to use additional functions, unless
you plan to avoid uses of printf() and friends.

-Peter
Nov 21 '07 #37
RoS
In data Tue, 20 Nov 2007 06:55:08 -0800 (PST), prady scrisse:
>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 is not portable goes well here but for your all computer could
cause possibly the format of your hard disk

#include <stdio.h>

unsigned i=1;
char *w=0;
unsigned ww=0;

/*
0ra, 4j
label:
*/
void fun0(unsigned j)
{unsigned *k=&j;
k-=1;
ww=*(unsigned*)k;
}

void fun3(unsigned j)
{unsigned *a=&j;
printf("%u\n", i); ++i;
if(i==j+1) return;
a-=1;
*(unsigned*)a=ww;
}
int main(void)
{fun0(0);
fun3(45);
return 0;
}
Nov 21 '07 #38
On 20 Nov, 14:55, prady <prady1...@gmail.comwrote:
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.
how about something like this

void printn (int n)
{
char s[] = " 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n\n";
printf ("%0.*s", 3 * n, s);
}

obviously it needs extending for numbers larger than 11.
The string may get rather large...
--
Nick Keighley
Nov 21 '07 #39
Peter Ammon wrote:
Chris Dollin wrote:
>Peter Ammon wrote:
>>Recursion is disallowed, but a function can call a separate but
otherwise identical function.

Thereby failing the requirement "/A/ C function" [emphasis mine]. Just
the one, Mrs Wembley.

I'm pretty sure you are permitted to use additional functions, unless
you plan to avoid uses of printf() and friends.
If you're allowed to use other functions are you allowed to use other
recursive functions? What if printf() is, on some execution path,
recursive, or calls a recursive function?
Nov 21 '07 #40
On 21 Nov, 09:21, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 20 Nov, 14:55, prady <prady1...@gmail.comwrote:
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.

how about something like this

void printn (int n)
{
char s[] = " 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n\n";
printf ("%0.*s", 3 * n, s);

}

obviously it needs extending for numbers larger than 11.
The string may get rather large...
for instance to handle all 5 digit numbers and smaller I estimate
the string would be >500,000 characters. This would break printf()
if integers are only 16-bit.

--
Nick Keighley
Nov 21 '07 #41
Peter Ammon wrote:
Chris Dollin wrote:
>Peter Ammon wrote:
>>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.

Recursion is disallowed, but a function can call a separate but
otherwise identical function.

Thereby failing the requirement "/A/ C function" [emphasis mine].
Just the one, Mrs Wembley.

I'm pretty sure you are permitted to use additional functions, unless
you plan to avoid uses of printf() and friends.
Clearly [ha] you can use /existing/ functions; it's in writing the new
one that the restrictions applies. That would be my reading.

--
All Alone Hedgehog
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Nov 21 '07 #42
RoS
In data Wed, 21 Nov 2007 09:27:16 +0100, RoS scrisse:
>In data Tue, 20 Nov 2007 06:55:08 -0800 (PST), prady scrisse:
>>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 is not portable goes well here but for your all computer could
cause possibly the format of your hard disk

#include <stdio.h>

unsigned i=1;
char *w=0;
unsigned ww=0;

/*
0ra, 4j
label:
*/
void fun0(unsigned j)
{unsigned *k=&j;
k-=1;
ww=*(unsigned*)k;
}

void fun3(unsigned j)
{unsigned *a=&j;
printf("%u\n", i); ++i;
if(i==j+1) return;
a-=1;
*(unsigned*)a=ww;
}
int main(void)
{fun0(0);
fun3(45);
return 0;
}
this also means that is possible to write fun3() that here return
whatever position has the last call for fun0()
afther that call. it not seems very useful
and my windows xp sys has reboot
Nov 22 '07 #43
hi all...

i get to know that the prog can be solved using "non-local" jumps. :)

-
prady
Nov 23 '07 #44
prady wrote:
hi all...

i get to know that the prog can be solved using "non-local" jumps. :)
See the sub-thread started by Richard Tobin. I'm with RH on this.
Nov 23 '07 #45
Chris Dollin wrote:
Peter Ammon wrote:
>Chris Dollin wrote:
>>Peter Ammon wrote:

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.
>
Recursion is disallowed, but a function can call a separate but
otherwise identical function.
Thereby failing the requirement "/A/ C function" [emphasis mine].
Just the one, Mrs Wembley.
I'm pretty sure you are permitted to use additional functions, unless
you plan to avoid uses of printf() and friends.

Clearly [ha] you can use /existing/ functions; it's in writing the new
one that the restrictions applies. That would be my reading.
Hmm. If one may not write additional functions, even if they satisfy
the requirements, but standard library functions that recurse or loop or
otherwise flagrantly violate the requirements are permitted, then I
present the following solution:

#include <stdio.h>
#include <stdlib.h>

int print(const void *a, const void *b) {
static int current, max;
if (b == NULL) {
max = *(int *)a;
void *ptr = malloc(max);
if (ptr) qsort(ptr, max, 1, print);
free(ptr);
}
else {
if (current < max) printf("%d\n", ++current);
}
return 0;
}

int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
print(&val, NULL);
return 0;
}
Nov 23 '07 #46
Peter Ammon said:
Chris Dollin wrote:
>Peter Ammon wrote:
>>Chris Dollin wrote:
Peter Ammon wrote:

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.
>>
Recursion is disallowed, but a function can call a separate but
otherwise identical function.
Thereby failing the requirement "/A/ C function" [emphasis mine].
Just the one, Mrs Wembley.
I'm pretty sure you are permitted to use additional functions, unless
you plan to avoid uses of printf() and friends.

Clearly [ha] you can use /existing/ functions; it's in writing the new
one that the restrictions applies. That would be my reading.

Hmm. If one may not write additional functions, even if they satisfy
the requirements, but standard library functions that recurse or loop or
otherwise flagrantly violate the requirements are permitted, then I
present the following solution:

#include <stdio.h>
#include <stdlib.h>

int print(const void *a, const void *b) {
static int current, max;
if (b == NULL) {
max = *(int *)a;
void *ptr = malloc(max);
if (ptr) qsort(ptr, max, 1, print);
free(ptr);
}
else {
if (current < max) printf("%d\n", ++current);
}
return 0;
}

int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
print(&val, NULL);
return 0;
}
************
APPLAUSE
************

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 23 '07 #47
Peter Ammon wrote:
Chris Dollin wrote:
>Peter Ammon wrote:
>>Chris Dollin wrote:
Peter Ammon wrote:

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.
>>
Recursion is disallowed, but a function can call a separate but
otherwise identical function.
Thereby failing the requirement "/A/ C function" [emphasis mine].
Just the one, Mrs Wembley.
I'm pretty sure you are permitted to use additional functions, unless
you plan to avoid uses of printf() and friends.

Clearly [ha] you can use /existing/ functions; it's in writing the new
one that the restrictions applies. That would be my reading.

Hmm. If one may not write additional functions, even if they satisfy
the requirements, but standard library functions that recurse or loop or
otherwise flagrantly violate the requirements are permitted, then I
present the following solution:

#include <stdio.h>
#include <stdlib.h>

int print(const void *a, const void *b) {
static int current, max;
if (b == NULL) {
max = *(int *)a;
void *ptr = malloc(max);
if (ptr) qsort(ptr, max, 1, print);
free(ptr);
}
else {
if (current < max) printf("%d\n", ++current);
}
return 0;
}

int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
print(&val, NULL);
return 0;
}
Well That is VERY good, really

May I include it in my tutorial I am writing about C?

It is such a nice program!

(Of course with all due credits)
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Nov 23 '07 #48
Peter Ammon <gersh...@splintermac.comwrote:
>>>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.

Hmm. If one may not write additional functions, even if
they satisfy the requirements, but standard library
functions that recurse or loop or otherwise flagrantly
violate the requirements are permitted, then I present the
following solution:
Clever, but it does have a fencepost error.
#include <stdio.h>
#include <stdlib.h>

int print(const void *a, const void *b) {
static int current, max;
if (b == NULL) {
max = *(int *)a;
void *ptr = malloc(max);
if (ptr) qsort(ptr, max, 1, print);
Mixed declarations aren't valid in C90, but more
importantly, there is no guarantee that sorting max
elements will require max comparisons, especially
if the array consists of identical elements.
free(ptr);
}
else {
if (current < max) printf("%d\n", ++current);
}
return 0;
}
int print(const void *a, const void *b)
{
static int current, max;
void *ptr;
if (b == NULL)
{
max = * (const int *) a;
if (max < 0) max = 0;
ptr = malloc(max + 1);
if (ptr) qsort(ptr, max + 1, 1, print);
free(ptr);
}
else if (current < max)
{
printf("%d\n", ++current);
}

return 0;
}
>
int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
print(&val, NULL);
return 0;
}
I realise this code is proof of concept only, but still...

#include <limits.h>

int main(int argc, char *argv[])
{
long val;
int ival;

if (argc >= 2)
{
val = strtol(argv[1], 0, 10);
ival = val < INT_MIN ? INT_MIN :
val INT_MAX ? INT_MAX :
val ;
print(&ival, NULL);
}

return 0;
}

--
Peter
Nov 23 '07 #49
Peter Ammon wrote:
Chris Dollin wrote:
>Peter Ammon wrote:
>>Chris Dollin wrote:
Peter Ammon wrote:

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.
>>
Recursion is disallowed, but a function can call a separate but
otherwise identical function.
Thereby failing the requirement "/A/ C function" [emphasis mine].
Just the one, Mrs Wembley.
I'm pretty sure you are permitted to use additional functions, unless
you plan to avoid uses of printf() and friends.
Clearly [ha] you can use /existing/ functions; it's in writing the new
one that the restrictions applies. That would be my reading.

Hmm. If one may not write additional functions, even if they satisfy
the requirements, but standard library functions that recurse or loop or
otherwise flagrantly violate the requirements are permitted, then I
present the following solution:

#include <stdio.h>
#include <stdlib.h>

int print(const void *a, const void *b) {
static int current, max;
if (b == NULL) {
max = *(int *)a;
void *ptr = malloc(max);
if (ptr) qsort(ptr, max, 1, print);
free(ptr);
}
else {
if (current < max) printf("%d\n", ++current);
}
return 0;
}

int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
print(&val, NULL);
return 0;
}
Wow, you got Richard Heathfield and jacob navia to agree on something! ;)

And it's a truly ingenious solution, too, in so many ways. I
congratulate thee.
Nov 23 '07 #50

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Bruno R. Dias | last post: by
8 posts views Thread by David Sachs | last post: by
56 posts views Thread by Dave Vandervies | last post: by
16 posts views Thread by makko | last post: by
1 post views Thread by Jeff Gerber | last post: by
2 posts views Thread by Michael Sutherland | last post: by
27 posts views Thread by Frederick Gotham | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.