473,242 Members | 3,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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 3583
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bruno R. Dias | last post by:
Perhaps it would be interesting to program a virtual machine simulating an ancient computer (such as the pdp-7). Then, it would be rather interesting to code for it (porting gcc to it maybe?). I...
15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
8
by: David Sachs | last post by:
The following program illustrates an interesting effect of the way C++ resolves function overloading. I have verified with a member of the C++ stardard committee that the output shown is...
1
by: jrmsmo | last post by:
Hi there, I have an interesting problem that maybe you pros can suggest how I solve. I'm working with a third party program that serializes an XML document (it was obviously not designed with schema...
56
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
16
by: makko | last post by:
Hello, anyone know how to writre a program that take a commandline formula and prints the calculated result? example; $program 1+(2x3(3/2))-8 reagrds; Makkko
1
by: Jeff Gerber | last post by:
/* This test program will give a "Value cannot be null" error. If the lock in this code is removed (or Monitor.Enter()) the program will run as expected. I have found no explaination in...
2
by: Michael Sutherland | last post by:
Here's an interesting problem I've come across. I'm writing a program that essentially generates random strings (its a simulator of the game Boggle) and sends them to a function that does a...
27
by: Frederick Gotham | last post by:
I thought it might be interesting to share experiences of tracking down a subtle or mysterious bug. I myself haven't much experience with tracking down bugs, but there's one in particular which...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.