Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

interesting C program

 
 
Ben Pfaff
Guest
Posts: n/a
 
      11-20-2007
Rachael <(E-Mail Removed)> writes:

> There's the silly solution:
>
> int i=1;
> /* and then write out INT_MAX times: */
> if (i<=N) {
> printf("%d\n",i);
> i++;
> }


Why use a variable then?

if (i <= 1)
puts("1");
if (i <= 2)
puts("2");
if (i <= 3)
puts("3");
....
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      11-20-2007
Richard Tobin wrote:

> In article <fhutv0$f2c$(E-Mail Removed)>,
> Chris Dollin <(E-Mail Removed)> wrote:
>
>>>> 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.

>
> [...]
>
>>Actually I believe it's portable and completely straightforward. Also
>>an utterly stupid way of printing 1-to-N lines, but that's a different
>>issue.

>
> Can you see any point to the prohibition of switch statements?


It might be to prevent the student from wasting their time thinking of a
way of using them; otherwise, no.

[I've now managed to turn my insight into code. Perhaps "completely
straightforward" was a little optimistic, eg there's an `if` in there
whose condition is always true ... I think there's room for improvement.]

--
Chris "and another one which isn't" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      11-20-2007
prady <(E-Mail Removed)> writes:

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


Need it be portable?

void function(int n)
{
char cmd[64];
sprintf(cmd, "seq %d", n);
system(cmd);
}
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-20-2007
Chris Dollin wrote:

> [I've now managed to turn my insight into code. Perhaps "completely
> straightforward" was a little optimistic, eg there's an `if` in there
> whose condition is always true ... I think there's room for improvement.]


There was.

Shiny! That fixed the 0 case, too!

--
Chris "money for old rope" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      11-20-2007
Ben Pfaff <(E-Mail Removed)> writes:

> Rachael <(E-Mail Removed)> writes:
>
>> There's the silly solution:
>>
>> int i=1;
>> /* and then write out INT_MAX times: */
>> if (i<=N) {
>> printf("%d\n",i);
>> i++;
>> }

>
> Why use a variable then?
>
> if (i <= 1)
> puts("1");
> if (i <= 2)
> puts("2");
> if (i <= 3)
> puts("3");
> ...


Because for a program which covers all integers from 0 to MAX_INT N is
the cut off.

 
Reply With Quote
 
Rachael
Guest
Posts: n/a
 
      11-20-2007
On 20 Nov, 15:55, Ben Pfaff <(E-Mail Removed)> wrote:

> Why use a variable then?
>
> if (i <= 1)
> puts("1");
> if (i <= 2)
> puts("2");
> if (i <= 3)
> puts("3");
> ...


Assuming you mean
if (N >= 1)
then yes, you're right.
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      11-20-2007
Ben Pfaff wrote:
> prady <(E-Mail Removed)> writes:
>
>> 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.

>
> Need it be portable?
>
> void function(int n)
> {
> char cmd[64];
> sprintf(cmd, "seq %d", n);
> system(cmd);
> }


If you're not worried about portability, then look at my program:

void _G(int n);

void f(int n) {
_G(n);
}

Naturally, _G() is to be provided by the implementation.
 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      11-20-2007
On Tue, 20 Nov 2007 06:55:08 -0800, 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.

Hint: look up the standard library functions setjmp and longjmp.


 
Reply With Quote
 
Johannes Bauer
Guest
Posts: n/a
 
      11-20-2007
Rachael schrieb:
> There's the silly solution:

[Snip]

That's what I thought first, but couldn't really believe they were
asking for that.

> I once failed an interview question very similar to this, because I
> couldn't imagine they were looking for such an inelegant "solution",
> but they were.


Most braindead assignment *ever*, seriously. Whoever asks these
questions should not be allowed to teach/have a leading position in HR.
BTW, did you get the job anyways?

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <4740ad67$0$3811$(E-Mail Removed)>
 
Reply With Quote
 
Rachael
Guest
Posts: n/a
 
      11-20-2007
On 20 Nov, 17:21, Johannes Bauer <(E-Mail Removed)> wrote:

> Most braindead assignment *ever*, seriously. Whoever asks these
> questions should not be allowed to teach/have a leading position in HR.
> BTW, did you get the job anyways?


The other questions in the interview were very interesting. I think
with that question they wanted to see whether I was blind to the
"braindead" solution when there were no other options.
I didn't get the job, but it's OK, because it was about an hour away
by train and my current job is 15 minutes away by bike
 
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