473,839 Members | 1,464 Online

# Programming Puzzle

I found these questions on a web site and wish to share with all of u
out there,Can SomeOne Solve these Porgramming puzzles.
Programming Puzzles

Some companies certainly ask for these things. Specially Microsoft.
Here are my favorite puzzles. Don't send me emails asking for the
solutions.

Q1 Write a "Hello World" program in 'C' without using a semicolon.
Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Q3 C/C++ : Exchange two numbers without using a temporary variable.
Q4 C/C++ : Find if the given number is a power of 2.
Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
Q6 C/C++ : Write a function in different ways that will return f(7) =
4 and f(4) = 7
Q7 Remove duplicates in array
Q8 Finding if there is any loop inside linked list.
Q9 Remove duplicates in an no key access database without using an
array
Q10 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.
Q11 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.
Q12 Swap two numbers without using a third variable.
Given an array (group) of numbers write all the possible sub groups of
this group.
Q14 Convert (integer) number in binary without loops.

Q3,12 are similar , Q7 is simple & I know there answer For the Rest
Nov 14 '05
271 20376
Dan Pop wrote in news:cb******** ***@sunnews.cer n.ch in comp.lang.c++:
However, if C++ allows one's complement and sign-magnitude, the
guarantees about plain char are a lie: because -0 and +0 are different
representations of the *same* value and you're effectively losing
information when reading a byte through a plain char: bytes with all
bits zero and bytes with all bits one will be read as having the
*same* value. And if you copy this value to another place, you have
no guarantee that the same representation will be used.

In C++ plain char is allowed to have the same representation and
symantics as unsigned char.

Its required that char and unsigned char be usable as "raw-memory".

Conclusion: on a one's compliment/signed magnitude machine char has
the same representation and symantics as unsigned char.
That is, assuming that plain char has the same semantics in both
languages: a type *different* from both signed char and unsigned char,
but sharing the semantics of one of them.

Yes.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 14 '05 #171
Ioannis Vranos wrote:
Ioannis Vranos wrote:
Jatinder wrote:
Q3 C/C++ : Exchange two numbers without using a temporary variable.

Isn't the bitwise solution safe only for unsigned integrals?

I just checked the standard, it is safe for both integral and
enumeration types.

....but it definitely is a nonsense for floating point numbers.
Therefore, there's no general way how to "Exchange two numbers without
using a temporary variable."

ld
Nov 14 '05 #172
Julie wrote:
Joona I Palaste wrote:
Of course it will break down if those variables
happen to share the same memory location, which can be the case if using
pointers and indirecting through them.

Explain to me how *two* variables can share the same memory address.

Nov 14 '05 #173
Dan Pop wrote:

In <40************ ***@nospam.com> Julie <ju***@nospam.c om> writes:
Dingo wrote:

Julie <ju***@nospam.c om> wrote in message news:<40******* ********@nospam .com>...
> Gordon Burditt wrote:
> >
> > >> >Not one, but *two* ways to do it have been
> > >> >shown in this thread. Of course it will break down if those variables
> > >> >happen to share the same memory location, which can be the case if using
> > >> >pointers and indirecting through them.
> > >
> > >Please describe (in code) a situation where two variables share the same memory
> > >location.
> >
> > A union?
>
> Nope -- a union is still a single variable, with just different ways to access
> it.
I wish to understand why you think a union member is not one.

I'll retract my statement -- I'll agree that a union does allow for two
variables to share the same memory address.

And, if you think even harder, you'll realise that it's the same thing
with pointers: *p and *q are two different variables, but they may share
the same memory address, depending on how the p and q variables have been
initialised (e.g. one may be pointing to one member of the union, the
other may be pointing to the other member of the union mentioned in the
union example :-)

Negative. I do not consider pointers or references that point to the same
variable to *be* the same variable. In your example, yes _p_ and _q_ are
variables, _*p_ and _*q_ are not, they merely (may) point to 0 or more
variables.
Nov 14 '05 #174
In <a5************ **************@ posting.google. com> k_*****@yahoo.c om (kal) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<cb******* ***@sunnews.cer n.ch>...
In <2k***********@ uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
>"Dan Pop" <Da*****@cern.c h> wrote in message
>news:cb******* ****@sunnews.ce rn.ch...
>> Consider, for example, 0 ^ 0, whose result (an int with all bits set)
>[...]
>
>*cough*

Indeed! A first class brain fart...

Ah! Merci beaucoup. I have been trying to understand that.

There is no shame in asking when you don't understand something...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #175
Risto Lankinen wrote:

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...

Could you please post the relevant code that sets up two variables that

share
the same memory location?

This is a good start:

Wha?! Forth? I don't see anything that even remotely describes how to set up
two variables that share the same memory location (_in_C_or_C++_, please).

When posting links, it is _most_ helpful if you first quote the relevant
portion(s), then post the referencing link...

Try again?
Nov 14 '05 #176
pete wrote:

Julie wrote:
Please describe (in code) a situation where two
variables share the same memory location.

In post
in the function n_sort, the variable (*node),
has the same address as either (tail) or (head) after this line:

Try again.

No *variable* is sharing the same address. Node, head, and tail all have
different addresses, unless you can say that any of the following is true:

It doesn't matter what node _points_ to, it matters where node _is_.

Under your logic, the following two variables:

int a = 1;
int b = 2;

become the _same_ same variable with the following:

b = 1;

Surely you don't now consider a and b the same _variable_, do you?

Remember, we are talking about _addresses_ of variables, not the _value_ of
variables.
Nov 14 '05 #177

Haven't we beaten this dead horse long enough???

-Howard
Nov 14 '05 #178
Howard wrote:

Haven't we beaten this dead horse long enough???

-Howard

No, just getting started.

Those that are not interested in the outcome or further discussion of this
Netscape (4.x), press 'K' and you will not be bothered any more w/ our
senseless blather on swapping and the (dis)similarity of variable addresses.
Nov 14 '05 #179
Julie posted:
Howard wrote:

Haven't we beaten this dead horse long enough???

-Howard

No, just getting started.

Those that are not interested in the outcome or further discussion of
newsreader. In Netscape (4.x), press 'K' and you will not be bothered
any more w/ our senseless blather on swapping and the (dis)similarity

I'm a variable.
-JKop
Nov 14 '05 #180

This thread has been closed and replies have been disabled. Please start a new discussion.