468,462 Members | 1,811 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,462 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
66 3306
Peter Nilsson wrote:
Peter Ammon <gersh...@splintermac.comwrote:
>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.
I can't see anywhere in the C standard a guarantee that the comparison
will be called at all; only that the answer will be correct. If the
implementation can determine that the comparison function always returns
0, I think it would be perfectly conforming to simply return from
qsort() having done nothing.

If we return to the real world then qsort() is probably implemented
using quicksort - which *does* guarantee at least n comparisons.
>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;
}
....and what happens if I enter -2? Admittedly the program doesn't break,
but it does try to allocate a large amount of memory (at least 65534
bytes) and output that many numbers, which I wouldn't say is the right
answer.
Nov 23 '07 #51
In article <fi**********@aioe.org>, Philip Potter <pg*@doc.ic.ac.ukwrote:
>I can't see anywhere in the C standard a guarantee that the comparison
will be called at all; only that the answer will be correct. If the
implementation can determine that the comparison function always returns
0, I think it would be perfectly conforming to simply return from
qsort() having done nothing.

If we return to the real world then qsort() is probably implemented
using quicksort - which *does* guarantee at least n comparisons.
*Any* sort (that doesn't magically know what the comparison function
returns) must do at least n-1 comparisons.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 23 '07 #52
jacob navia wrote:
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)
Wow, I'm flattered! I didn't think my latest delivery of smart-ass
would be so well received!

You may use in any context you like, and punish me with attribution if
you must.

-Peter
Nov 24 '07 #53
Richard Tobin wrote:
In article <fi**********@aioe.org>, Philip Potter <pg*@doc.ic.ac.ukwrote:
>I can't see anywhere in the C standard a guarantee that the comparison
will be called at all; only that the answer will be correct. If the
implementation can determine that the comparison function always returns
0, I think it would be perfectly conforming to simply return from
qsort() having done nothing.

If we return to the real world then qsort() is probably implemented
using quicksort - which *does* guarantee at least n comparisons.

*Any* sort (that doesn't magically know what the comparison function
returns) must do at least n-1 comparisons.
This is the point Peter Nilsson was making. n-1 wasn't enough for the
algorithm to work, it needed n.
Nov 24 '07 #54
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;
}
`print` is recursive.

No biscuit.

--
Indirect Isn't Isn't Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

Nov 24 '07 #55
Chris Dollin said:

<snip>
`print` is recursive.
That's hardly Peter's fault. He's just calling a standard library function.
No biscuit.
No biscuit, no beer. Give the man his biscuit.

--
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 24 '07 #56
In article <NE*******************@fe2.news.blueyonder.co.uk >, Chris
Dollin <eh@electrichedgehog.netwrote on Saturday 24 Nov 2007 2:24 pm:
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;
}

`print` is recursive.
You said that using existing functions is okay. That's what he is doing;
using qsort(). The fact that it calls print() itself is okay... by your
own rules up-thread.

Nov 24 '07 #57
Richard Heathfield wrote:
Chris Dollin said:

<snip>
>`print` is recursive.

That's hardly Peter's fault. He's just calling a standard library
function.
I have no problem with that. But it makes `print` recursive,
and hence disallowed by the original specification.

--
Focus Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

Nov 24 '07 #58
santosh wrote:
In article <NE*******************@fe2.news.blueyonder.co.uk >, Chris
Dollin <eh@electrichedgehog.netwrote on Saturday 24 Nov 2007 2:24 pm:
(fx:extended-snip)
>>>>>>>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.
>>#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;
}

`print` is recursive.

You said that using existing functions is okay. That's what he is doing;
using qsort(). The fact that it calls print() itself is okay... by your
own rules up-thread.
"My" rules are a mere clarification about how many functions one is
allowed to write to solve the problem; the original "should not use"
recursion (which I have preserved above) isn't thereby abdicated.

`print` is recursive, and hence disallowed; indirect recursion is
still recursion; calls via function pointers are still calls;
recursion via qsort (which, by the way, I was dead impressed
by) is the solution-killer.

--
Fix Hedgehog
Meaning precedes definition.

Nov 24 '07 #59
Chris Dollin <eh@electrichedgehog.netwrites:
Peter Ammon wrote:
<snip>
>>>>>prady wrote:
>>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.
<snip>
>#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;
}

`print` is recursive.

No biscuit.
I think it also pushes the requirement that "N is a int parameter to
the function" (sic) a bit far. Still, neither is a fatal flaw to the
deliciously sneaky idea:

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

int max;

int print_one(const void *a, const void *b)
{
static int current;
if (current < max) printf("%d\n", ++current);
return 0;
}

void print(int n)
{
void *ptr;
if ((max = n) 0 && (ptr = malloc(n)))
qsort(ptr, n, 1, print_one);
free(ptr);
}

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

--
Ben.
Nov 24 '07 #60
Ben Bacarisse wrote:
I think it also pushes the requirement that "N is a int parameter to
the function" (sic) a bit far. Still, neither is a fatal flaw to the
deliciously sneaky idea:
(fx:snip)
int print_one(const void *a, const void *b)
(fx:ditto)
void print(int n)
Two functions. In this (sub)thread, one of the Dollins claims
that's out-of-spec, since the original post called for /a/
function.

--
A /And/ B Hedgehog
The shortcuts are all full of people using them.

Nov 24 '07 #61
Chris Dollin <eh@electrichedgehog.netwrites:
Ben Bacarisse wrote:
>I think it also pushes the requirement that "N is a int parameter to
the function" (sic) a bit far. Still, neither is a fatal flaw to the
deliciously sneaky idea:

(fx:snip)
>int print_one(const void *a, const void *b)

(fx:ditto)
>void print(int n)

Two functions. In this (sub)thread, one of the Dollins claims
that's out-of-spec, since the original post called for /a/
function.
Harsh, very harsh.

--
Ben.
Nov 24 '07 #62
Ben Bacarisse wrote:
Chris Dollin <eh@electrichedgehog.netwrites:
>Ben Bacarisse wrote:
>>I think it also pushes the requirement that "N is a int parameter to
the function" (sic) a bit far. Still, neither is a fatal flaw to the
deliciously sneaky idea:

(fx:snip)
>>int print_one(const void *a, const void *b)

(fx:ditto)
>>void print(int n)

Two functions. In this (sub)thread, one of the Dollins claims
that's out-of-spec, since the original post called for /a/
function.

Harsh, very harsh.
What would you expect from someone who had already devised a
single-function solution? They're probably just ratty about
people who's solutions aren't just like their own.

--
What? Hedgehog
Notmuchhere: http://www.electrichedgehog.net/

Nov 25 '07 #63
Chris Dollin wrote:
>
Richard Heathfield wrote:
Chris Dollin said:

<snip>
`print` is recursive.
That's hardly Peter's fault. He's just calling a standard library
function.

I have no problem with that. But it makes `print` recursive,
and hence disallowed by the original specification.
Must print() call qsort() to itself? Couldn't it call a different
function?

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

static int mymax; /* Specs didn't say "no global variables" */

int print(const void *a, const void *b) {
static int current = 0;
if (current < mymax) printf("%d\n", ++current);
return 0;
}

void doit(int i) {
void *ptr = malloc(i+1); /* Should have test for failure */
mymax = i;
qsort(ptr,mymax+1,1,print);
free(ptr);
}

int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
doit(val);
return 0;
}
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 26 '07 #64
Why not something more simple ?

Like a sub calling itself.

------------8<----------------------
#include<stdio.h>

void printchar(char *mychar, int number)
{
int newnumb;
printf("%s\n", mychar);
if (number 1) {
newnumb = number - 1;
printchar(mychar, newnumb);
}
}

int main(void)
{
printchar("a",10); // Print character "a", 10 times! ("a" can be a
character string too eg: "hello world")
return 0;
}
------------------------------------
Nov 26 '07 #65
Benoit Lefebvre wrote:
Why not something more simple ?

Like a sub calling itself.
Because recursion was one of the many things that were prohibited.
Nov 26 '07 #66
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.
why not "add" ifs? :)
>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;
}

int fun3(unsigned j)
{unsigned *a=&j;
printf("%u\n", i); ++i;
i!=j+1 && (a-=1)!=0 && (*(unsigned*)a=ww)!=0;
return 0;
}
int main(void)
{fun0(0);
fun3(45);
return 0;
}
Dec 3 '07 #67

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.