interesting C program

N

Nick Keighley

could any one solve the following C program. If any one knows the
answer please post it

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

Chris Dollin

Peter said:
Chris said:
Peter said:
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.
 
R

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:

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
 
M

Mark Bluemel

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

Peter Ammon

Chris said:
Peter said:
Chris said:
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;
}
 
R

Richard Heathfield

Peter Ammon said:
Chris said:
Peter said:
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
************
 
J

jacob navia

Peter said:
Chris said:
Peter said:
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)
 
P

Peter Nilsson

Peter Ammon said:
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;
}
 
P

Philip Potter

Peter said:
Chris said:
Peter said:
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.
 
P

Philip Potter

Peter said:
Clever, but it does have a fencepost error.


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

Richard Tobin

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
 
P

Peter Ammon

jacob said:
Peter said:
Chris said:
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
 
P

Philip Potter

Richard said:
*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.
 
C

Chris Dollin

Peter said:
Chris said:
Peter said:
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.
 
S

santosh

Chris said:
Peter said:
Chris said:
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.
 
C

Chris Dollin

Richard said:
Chris Dollin said:



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

Chris Dollin

santosh said:
(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.
 
B

Ben Bacarisse

Chris Dollin said:
Peter Ammon wrote:
#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;
}
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top