![]() |
|
|
|
#11 |
|
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?-*s ),putchar(main(c,k+=*s-c*-1,s+1)) Pieter Droogendijk |
|
|
|
|
#12 |
|
Posts: n/a
|
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 |
|
|
|
#13 |
|
Posts: n/a
|
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 |
|
|
|
#14 |
|
Posts: n/a
|
> 4. Multiply x by 7 without using multiplication (*) operator.
See the post by krazyman a week ago. -- Martijn Haak http://www.serenceconcepts.nl Martijn |
|
|
|
#15 |
|
Posts: n/a
|
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 |
|
|
|
#16 |
|
Posts: n/a
|
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 |
|
|
|
#17 |
|
Posts: n/a
|
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 |
|
|
|
#18 |
|
Posts: n/a
|
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 |
|
|
|
#19 |
|
Posts: n/a
|
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 |
|
|
|
#20 |
|
Posts: n/a
|
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 |
|