![]() |
|
|
|
#1 |
|
I will be greatful if any one answers these questions?
Thank You. 1. Write a "Hello World" program in 'C' without using a semicolon. 2. Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1; 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. Girish Pal Singh |
|
|
|
|
#2 |
|
Posts: n/a
|
Girish Pal Singh wrote:
> I will be greatful if any one answers these questions? Do your own homework. -- Hallvard |
|
|
|
#3 |
|
Posts: n/a
|
In article <>,
Girish Pal Singh wrote: > I will be greatful if any one answers these questions? These questions? > Thank You. You're welcome. > 1. Write a "Hello World" program in 'C' without using a semicolon. Ummm--true. > 2. Write a C++ program without using any loop (if, for, while etc) to > print numbers from 1 to 100 and 100 to 1; Er--false. > 3. Find if the given number is a power of 2. True. > 4. Multiply x by 7 without using multiplication (*) operator. True. > 5. Write a function in different ways that will return f(7) = 4 and > f(4) = 7 False? > 6. Remove duplicates in array True. > 7. Finding if there is any loop inside linked list. False. > 8. Remove duplicates in an no key access database without using an > array False. > 9. Convert (integer) number in binary without loops. True. > 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. True. > 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. False. > 13. Swap two numbers without using a third variable. True. How'd I do? -- Neil Cerutti |
|
|
|
#4 |
|
Posts: n/a
|
"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. > Thank You. You're welcome. < snip > -- Bob Day |
|
|
|
#5 |
|
Posts: n/a
|
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. Try using the token pasting operator on the comma and period. > 2. Write a C++ program without using any loop (if, for, while etc) to > print numbers from 1 to 100 and 100 to 1; I asked my computer, and it came up with this: #include <stdio.h> int main(void) { int i; puts("#include <stdio.h>\nint main(void) {"); for (i=1; i<=100; i++) printf("puts(\"%d\");\n", i); for (i=100; i>=1; i--) printf("puts(\"%d\");\n", i); puts("return 0;\n}"); return 0; } If my computer can solve this, you can too. > > 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. > 4. Multiply x by 7 without using multiplication (*) operator. x*=7; > 5. Write a function in different ways that will return f(7) = 4 and > f(4) = 7 foo(x) { return x==4 ? 7 : 4; } foo2(x) { return x==7 ? 4 : 7; } > 6. Remove duplicates in array void removeDuplicates(int* array, int_t count) { int i; for (i=0; i < count; i++) array[i]=i; } > 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). > 8. Remove duplicates in an no key access database without using an > array What the heck is a "no key access database?" > 9. Convert (integer) number in binary without loops. Convert it to what? > 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. "" (The program is between the quotes.) > 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. And? > 13. Swap two numbers without using a third variable. Easy. int main(void) { int first_number=1; int second_number=2; int third_variable; int fourth_temp; fourth_temp=first_number; first_number=second_number; second_number=fourth_temp; return 0; } Notice I never use the third variable. Want proof? peterammon: ~ ) cc -W -Wall test.c test.c: In function `main': test.c:4: warning: unused variable `third_variable' -Peter |
|
|
|
#6 |
|
Posts: n/a
|
"Girish Pal Singh" <> wrote in message > I will be greatful if any one answers these questions? > I wonder what you tutor is looking for here. Maybe these are end of term "fun" questions; > > 1. Write a "Hello World" program in 'C' without using a semicolon. > Hint - use an "if" statement. > > 2. Write a C++ program without using any loop (if, for, while etc) to > print numbers from 1 to 100 and 100 to 1; > Brute force this is easy. Try clc++ for further hints. > > 3. Find if the given number is a power of 2. > You need to check that only one bit is set. > > 4. Multiply x by 7 without using multiplication (*) operator. > This is called backcracking x * constant can always be replaced by (x << a) + (x << b) ... > > 5. Write a function in different ways that will return f(7) = 4 and > f(4) = 7 > presumably without using an if statement. You need to look at bitwise operators. > > 6. Remove duplicates in array > This is actually a real task. It can be done by using nested loops. > > 7. Finding if there is any loop inside linked list. > You need to store the address of one of the nodes, then check if you revisit it. > > 8. Remove duplicates in an no key access database without using an > array > Got me here. "Never bullshit your way into a databse job". > > 9. Convert (integer) number in binary without loops. > Unroll the loop > > 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. > look up "quine" on the web. > > 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. > Your question? > > 13. Swap two numbers without using a third variable. > This is a stupid hack. I'll give you this one. a ^= b; a ^= b; a ^= b. |
|
|
|
#7 |
|
Posts: n/a
|
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. int main(void) { puts ("Hello, world!") ., return 0 ., } Unfortunately, this example won't compile or execute. But the problem statement only asked that the program be written, not that it be correct, so that's all right. > 2. Write a C++ program without using any loop (if, for, while etc) to > print numbers from 1 to 100 and 100 to 1; Sorry; this is comp.lang.c, not comp.lang.c++. Ask your C++ questions somewhere else -- comp.lang.snobol, perhaps. > 3. Find if the given number is a power of 2. int is_power_of_2(int given_number) { return given_number > 0; } > 4. Multiply x by 7 without using multiplication (*) operator. int multiply_by_7(int x) { return x / 0.14285714285714286; } > 5. Write a function in different ways that will return f(7) = 4 and > f(4) = 7 int f(int x) { return x == 4 ? 7 : 4; } int f(int x) { return x == 7 ? 4 : 7; } > 6. Remove duplicates in array for (i = 0; i < array_elements; ++i) array[i] = i; > 7. Finding if there is any loop inside linked list. int has_a_loop(struct list_node *ptr) { for ( ; ptr != NULL; ptr = ptr->next) ; return 0; } If the list is loop-free, this function returns zero ("false"). If there *is* a loop, the function does not return zero. > 8. Remove duplicates in an no key access database without using an > array Databases are off-topic in comp.lang.c. Try another newsgroup, like comp.graphics.algorithms. > 9. Convert (integer) number in binary without loops. void to_binary(unsigned int x) { static const char *binary[] = { "0", "1", "10", "11", "100", /* complete in the obvious way */ }; puts (binary[x]); } > 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. This is impossible. If a program does not output its own source file, it hasn't output its own source, merely something that happens to look exactly like it. The fact that two files have identical contents doesn't make them the same file, just as the fact that two dollar bills may look identical but aren't the same dollar bill -- in fact, if they *do* look identical it follows that at least one is a forgery. Q.E.D. > 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. void play_and_win(void) { puts ("I go first. I choose the four sixes,\n" "a four, a two, and a one, and add them\n" "all to the total. The total is now\n" "thirty-one, so I win! Hooray!"); exit(0); } > 13. Swap two numbers without using a third variable. void swap(int x, int y) { printf ("x = %d, y = %d\n", y, x); } -- |
|
|
|
#8 |
|
Posts: n/a
|
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). int linkListLoop (struct linkedList * x) { x->next = x; return 1; /* Indicating that yes it has a loop in it. */ } -- Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sourceforge.net/ |
|
|
|
#9 |
|
Posts: n/a
|
"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 |
|
|
|
#10 |
|
Posts: n/a
|
>> 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" -- Martijn Haak http://www.serenceconcepts.nl |
|