Puzzles

G

Girish Pal Singh

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

Neil Cerutti

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?
 
B

Bob Day

Girish Pal Singh said:
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
 
P

Peter Ammon

Girish said:
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;
}
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
 
M

Malcolm

Girish Pal Singh said:
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.
 
E

Eric Sosman

Girish said:
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;
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);
}
 
P

Paul Hsieh

Peter Ammon said:
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. */
}
 
A

Andy Zhang

Peter Ammon said:
Girish Pal Singh wrote:

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;
}
 
M

Martijn

3. Write C code to divide by 15 efficiently without
Impress me; write a C routine to divide by an arbitrary integer,
efficiently, without a divide operator. :)

define "efficiently" :)
 
P

Pieter Droogendijk

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 :p
 
E

Emmanuel Delahaye

In said:
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?
 
J

Joona I Palaste

Girish Pal Singh said:
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 ([email protected]) ---------------------------\
| 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
 
M

Martijn

4. Multiply x by 7 without using multiplication (*) operator.

See the post by krazyman a week ago.
 
T

Tim Woodall

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");
What the heck is a "no key access database?"

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

Tim.
 
M

MCheu

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

pete

Andy said:
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);
}
 
P

Peter Ammon

Andy said:
Easier way would be to use log:

#include <math.h>

int isPowerOf2(int x)
{
return (fmod(log10(x), log10(2))) == 0;
}

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
 
K

Kevin Handy

Kelsey said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top