interesting C program

C

Chris Dollin

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

Ben Bacarisse

Chris Dollin said:
(fx:snip)


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

Chris Dollin

Ben said:
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.
 
K

Kenneth Brody

Chris said:
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:[email protected]>
 
B

Benoit Lefebvre

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;
}
------------------------------------
 
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:

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

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top