Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > interesting C program

Reply
Thread Tools

interesting C program

 
 
Chris Dollin
Guest
Posts: n/a
 
      11-24-2007
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.

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-24-2007
Chris Dollin <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      11-25-2007
Ben Bacarisse wrote:

> Chris Dollin <(E-Mail Removed)> writes:
>
>> 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/

 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      11-26-2007
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: <(E-Mail Removed)>


 
Reply With Quote
 
Benoit Lefebvre
Guest
Posts: n/a
 
      11-26-2007
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;
}
------------------------------------
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-26-2007
Benoit Lefebvre wrote:
> Why not something more simple ?
>
> Like a sub calling itself.


Because recursion was one of the many things that were prohibited.
 
Reply With Quote
 
RoS
Guest
Posts: n/a
 
      12-03-2007
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;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An interesting c program for beginners apaticul@gmail.com C Programming 2 05-06-2008 10:58 AM
veRified: Interesting FACTORISE program, by trial division. + Fermat s method.Casio fx-82 MS schools sci-calc. don.lotto@paradise.net.nz NZ Computing 7 07-26-2007 06:26 AM
Interesting day.... bigal The Lounge 6 11-23-2005 05:58 AM
"Interesting" behavior with aggregates Tim Hubberstey VHDL 6 06-30-2004 06:38 AM
in response to an interesting thread catchmerevisited Firefox 2 05-08-2004 10:57 AM



Advertisments