Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - Puzzles

 
Thread Tools Search this Thread
Old 07-26-2003, 01:03 PM   #11
Default Re: Puzzles


On Fri, 25 Jul 2003 20:08:02 -0700
Kelsey Bjarnason <> wrote:

> On Fri, 25 Jul 2003 17:40:48 +0000, Bob Day wrote:
>
> > "Girish Pal Singh" <> wrote in message
> > news: om...
> >> I will be greatful if any one answers these questions?

> >
> > 1. Do your own homework.
> >
> > 2. It's 'grateful', not 'greatful'.
> >
> > 3. Write C code to divide by 15 efficiently without
> > using a divide operator.

>
> Impress me; write a C routine to divide by an arbitrary integer,
> efficiently, without a divide operator.
>
>

how about this for a symmetrically rounded division routine for unsigned
integers:

typedef unsigned int u;
u uidiv(u D,u d){u q=0,c=1;for(;d<D
;d<<=1,c++);for(;c;c--,d>>=1)q<<=1,
q+=(d<=D?D-=d,1:0);return q+(D>d);}

if you want efficiency, make D,d and q registers

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*sputchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1))s=0);return!s?10:10+*k;}


Pieter Droogendijk
  Reply With Quote
Old 07-26-2003, 07:53 PM   #12
Emmanuel Delahaye
 
Posts: n/a
Default Re: Puzzles
In 'comp.lang.c', (Girish Pal Singh) wrote:

> I will be greatful if any one answers these questions?
> Thank You.
> 1. Write a "Hello World" program in 'C' without using a semicolon.


It's not a question.

> 2. Write a C++ program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;


What is C++?

> 3. Find if the given number is a power of 2.
> 4. Multiply x by 7 without using multiplication (*) operator.
> 5. Write a function in different ways that will return f(7) = 4 and
> f(4) = 7
> 6. Remove duplicates in array
> 7. Finding if there is any loop inside linked list.
> 8. Remove duplicates in an no key access database without using an
> array
> 9. Convert (integer) number in binary without loops.
> 10. Write a program whose printed output is an exact copy of the
> source. Needless to say, merely echoing the actual source file is
> not allowed.
> 11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
> each player selects a number and adds it to the total. Once a
> number is used, it must be removed from the pool. The winner is the
> person whose number makes the total equal 31 exactly.
> 13. Swap two numbers without using a third variable.


None of thee are questions.

Do you have a question about the C language?

--
-ed- [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/


Emmanuel Delahaye
  Reply With Quote
Old 07-26-2003, 08:13 PM   #13
Joona I Palaste
 
Posts: n/a
Default Re: Puzzles
Girish Pal Singh <> scribbled the following:
> I will be greatful if any one answers these questions?
> Thank You.


Please tell me when your homework is due, and I'll provide the answers
immediately after that time.

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text


Joona I Palaste
  Reply With Quote
Old 07-26-2003, 11:02 PM   #14
Martijn
 
Posts: n/a
Default Re: Puzzles
> 4. Multiply x by 7 without using multiplication (*) operator.

See the post by krazyman a week ago.

--
Martijn Haak
http://www.serenceconcepts.nl




Martijn
  Reply With Quote
Old 07-27-2003, 11:56 AM   #15
Tim Woodall
 
Posts: n/a
Default Re: Puzzles
On Fri, 25 Jul 2003 11:15:09 -0700,
Peter Ammon <> wrote:
>> 7. Finding if there is any loop inside linked list.

>
> I couldn't come up with a tongue in cheek but correct answer for this
> one. Anyone want to take it? (The usual algorithm is to traverse the
> list at different speeds and see if you ever meet up).
>

if(strstr("linked list","loop")) printf("yes\n");

>> 8. Remove duplicates in an no key access database without using an
>> array

>
> What the heck is a "no key access database?"


No idea but
DROP DATABASE <dbname>;
ought to do the trick.

Tim.


--
God said, "div D = rho, div B = 0, curl E = - @B/@t, curl H = J + @D/@t,"
and there was light.

http://tjw.hn.org/ http://www.locofungus.btinternet.co.uk/


Tim Woodall
  Reply With Quote
Old 07-27-2003, 08:23 PM   #16
MCheu
 
Posts: n/a
Default Re: Puzzles
On 25 Jul 2003 10:23:29 -0700, (Girish Pal
Singh) wrote::

>I will be greatful if any one answers these questions?
>Thank You.


Do a google search. Homework questions come up quite frequently, and
while against the general consensus, some people do post answers
occasionally (the questions are quite common homework questions).

If you're going to use those to cheat though, you should at least look
them over and be prepared to do some debugging. Some are
intentionally wrong (though not always obviously so), others work but
are so obfuscated that you'd have to prove to the TA that you know how
it works to get the grade for it.

All things considered, it might actually be easier to do it yourself.
Might save you some time in studying for the exam later too.
----------------------------------------
Thanks,

MCheu


MCheu
  Reply With Quote
Old 07-28-2003, 04:39 AM   #17
pete
 
Posts: n/a
Default Re: Puzzles
Andy Zhang wrote:
>
> "Peter Ammon" <> wrote in message
> news:bfrs5g$9le$...
> > Girish Pal Singh wrote:

> <snip>
> > > 3. Find if the given number is a power of 2.

> >
> > int isPowerOf2(int x) {
> > int i;
> > for (i=0; x > 0; i++) {
> > if (x==1) return 1;
> > if (isPowerOf2(i)) x-=i;
> > }
> > return 0;
> > }
> >
> > I am tremendously proud of this solution.
> > Finding such a terse solution
> > with disgustingly exponential time isn't easy.
> >

>
> Easier way would be to use log:
>
> #include <math.h>
>
> int isPowerOf2(int x)
> {
> return (fmod(log10(x), log10(2))) == 0;
> }


This is the simple way:

int isPowerOf2(unsigned n)
{
return n && !(n & n - 1);
}

--
pete


pete
  Reply With Quote
Old 07-28-2003, 09:10 AM   #18
Steve Zimmerman
 
Posts: n/a
Default Re: Puzzles
Kelsey Bjarnason wrote:

> On Sat, 26 Jul 2003 13:51:16 +0200, Martijn wrote:
>
>
>>>>3. Write C code to divide by 15 efficiently without
>>>> using a divide operator.
>>>>
>>>Impress me; write a C routine to divide by an arbitrary integer,
>>>efficiently, without a divide operator.
>>>

>>define "efficiently"
>>

>
> "Fast". Neener, neener, neener.
>
>
>


Cool Marsha Clark reference. I remember that: "Neener, neener, neener."
heh heh



Steve Zimmerman
  Reply With Quote
Old 07-28-2003, 10:16 PM   #19
Peter Ammon
 
Posts: n/a
Default Re: Puzzles
Andy Zhang wrote:

> "Peter Ammon" <> wrote in message
> news:bfrs5g$9le$...
>
>>Girish Pal Singh wrote:

>
> <snip>
>
>>>3. Find if the given number is a power of 2.

>>
>>int isPowerOf2(int x) {
>> int i;
>> for (i=0; x > 0; i++) {
>> if (x==1) return 1;
>> if (isPowerOf2(i)) x-=i;
>> }
>> return 0;
>>}
>>
>>I am tremendously proud of this solution. Finding such a terse solution
>>with disgustingly exponential time isn't easy.
>>

>
>
> Easier way would be to use log:
>
> #include <math.h>
>
> int isPowerOf2(int x)
> {
> return (fmod(log10(x), log10(2))) == 0;
> }
>
> --
> Andy Zhang


On my machine, this function fails to report many values as being powers
of 2, including 8, 32, 64, 128, 512, 1024, 2048.... Don't trust
floating point precision.

-Peter



Peter Ammon
  Reply With Quote
Old 08-07-2003, 07:24 PM   #20
Kevin Handy
 
Posts: n/a
Default Re: Puzzles
Kelsey Bjarnason wrote:
> On Fri, 25 Jul 2003 17:40:48 +0000, Bob Day wrote:
>
>
>>"Girish Pal Singh" <> wrote in message
>>news: .com...
>>
>>>I will be greatful if any one answers these questions?

>>
>>1. Do your own homework.
>>
>>2. It's 'grateful', not 'greatful'.
>>
>>3. Write C code to divide by 15 efficiently without
>> using a divide operator.

>
>
> Impress me; write a C routine to divide by an arbitrary integer,
> efficiently, without a divide operator.
>
>


int divide(int a, int b)
{
return (int)pow(10.0, log10((double)a)-log10((double)b));
}

assuming efficient hardware assisted pow, log10, conversions,
values passed being in proper range, etc.


Now define efficient, or any additional silly limitations.




Kevin Handy
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46