473,854 Members | 1,584 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 20410
In <YX************ *******@newssvr 27.news.prodigy .com> "Mabden" <mabden@sbc_glo bal.net> writes:
"Michael" <mi************ @excite.com> wrote in message
news:20******* *************** ****@posting.go ogle.com...
> Please describe (in code) a situation where two variables share the samememory > location.
>

Dan you need to try stuff for yourself

Oohhhh.. you're arguing with Dan....

Nope, he isn't, but you're way too stupid to realise that. Clue: he was
replying to a request made by Julie and incorrectly mentioning my name.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #251
In comp.lang.c Julie <ju***@nospam.c om> wrote:
Michael wrote:
> Please describe (in code) a situation where two variables share the same memory
> location.
> Dan you need to try stuff for yourself

#include <stdio.h>

int main(void){
int x=10, *p;
p=&x;
printf("%p, %p\n",(void *)p,(void *)&x);
return 0;
}

You need to think a little harder: You have two variables: x and p. Are you saying that &x == &p? I sure hope
not.

That is not what he is saying. You are failing to grasp that *p is a
variable. Perhaps this quote from page 45 of K&R2 will enlighten you:

"...The increment and decrement operators can only be applied
to variables..."

This, in my mind, would mean that *p can be considered a variable, at
least part of the time. Technically, of course, *p can hardly be
considered a variable when p does not contain the address of a valid
object.

--
Alex Monjushko (mo*******@hotm ail.com)
Nov 14 '05 #252
You have two variables: x and p. Are you saying that &x == &p? I sure hope
not.

Of course, you can easily construct that sort of aliasing:

int a, &b = a;
assert( &a == &b );

I suspect the point of the thread was something else.

Also, FWIW, optimizers routinely fold objects like a and b below (i.e.,
they can occupy the same memory address) because the optimizer can prove
that a conforming program can't tell because their uses don't overlap:

#include <stdio.h>

int main() {
int a = 10;
printf( "%d", a );
int b = 20;
printf( "%d", b );
}

Herb

---
Herb Sutter (www.gotw.ca)

Convener, ISO WG21 (C++ standards committee) (www.gotw.ca/iso)
Contributing editor, C/C++ Users Journal (www.gotw.ca/cuj)
Visual C++ architect, Microsoft (www.gotw.ca/microsoft)
Nov 14 '05 #253
Herb Sutter <hs*****@gotw.c a> writes:
[...]
Also, FWIW, optimizers routinely fold objects like a and b below (i.e.,
they can occupy the same memory address) because the optimizer can prove
that a conforming program can't tell because their uses don't overlap:

#include <stdio.h>

int main() {
int a = 10;
printf( "%d", a );
int b = 20;
printf( "%d", b );
}

To expand on that point a bit, an optimizer can fold a and b because
it can prove that *this particular* program can't tell the difference
(and it's allowed to assume, for purposes of its proof, that the
program doesn't exhibit undefined behavior).

A conforming program could compare the addresses of a and b and expect
them to be different, but that would be a different program, and one
in which the optimizer wouldn't be allowed to fold a and b.

Actually, I'm making an assumption that I'm not 100% certain is
correct. Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("address es are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?

This is cross-posted to comp.lang.c and comp.lang.c++. If you're
the appropriate group.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #254
Keith Thompson wrote:
[...]

Fellows we got in the same stuff again. Actually the wrong side of the
subject has been erased. At first cross-posting in clc++ and clc has
nothing useful to provide. They are separate languages. Regarding C++ as

The C++ standard says:

"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."

"An object is a region of storage. [Note: A function is not an object,
regardless of whether or not it occupies storage in the way that objects
do. ] An object is created by a definition (3.1), by a new-expression
(5.3.4) or by the implementation (12.2) when needed."
So in terminology, a variable can only denote one object, which is
always different from another object.
The original question becomes "what if we pass the same object to the
swap function?".
If you do not use the correct terminology, you will never reach a
conclusion, since both of you use your own terminologies.
----

A pointer is a variable that denotes an object (which stores addresses).

A dereferenced pointer is not a variable (since it is not introduced by
the declaration of an object), but represents an object.
As I said, the original question becomes: "what if we pass the same
object to the swap function?".

It would be nice if you tried to communicate on this basis.

Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Nov 14 '05 #255
In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes:
... Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("address es are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?

In C at least, yes it does. (I would be quite surprised if the
answer changes for C++.) Note that the compiler *can* still combine
the underlying storage space for "a" and "b", as long as it *also*
pretends that &a != &b even though then &a == &b. For instance,
suppose the compiler first tries to optimize the printf() call.
At this time, "a" and "b" are still separate entities in the
compiler's internal representation (e.g., separate RTL pseudo-registers
inside GCC). Thus the result of &a==&b is a known constant 0 (in
C). The next step is to replace:

printf("address es are %s\n", 0 ? "equal" : "unequal");

with:

and perhaps even optimize this into:

Now that this has been done, there is no longer any occurrence of
&a or &b, so now the entities "a" and "b" can be folded together
to occupy the same hard-register-or-stack-slot. So now &a==&b even
though the code that prints whether &a==&b says they are not!
(Of course, if they are in a "hard register" they may not have an
address at all; and in the case of GCC, at least, constant propagation
would result in their removal entirely, at this point.)

If you were to replace the above printf() call with:

printf("address es are %p and %p\n", (void *)&a, (void *)&b);

the system would have to print two different numbers, in general,
and this would probably preclude the sort of folding described
here.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #256
Chris Torek <no****@torek.n et> writes:
In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes:
... Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("address es are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?

In C at least, yes it does. (I would be quite surprised if the

I believe you, but how does the standard imply this?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #257
Ioannis Vranos wrote:

Keith Thompson wrote:
> [...]
Fellows we got in the same stuff again. Actually the wrong side of the
subject has been erased. At first cross-posting in clc++ and clc has
nothing useful to provide. They are separate languages. Regarding C++ as

The C++ standard says:

Fortunately, comp.lang.c* doesn't have to restrict itself to just specifically
what is contained/defined within the standard. Such limited discussions are
appropriate in comp.std.c*; here we can talk about the language, as defined by
the standard.
"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."
Fine -- but what that is saying is that when you create an object, you also
introduce a variable. The opposite is not necessarily true: introducing a
variable does *NOT* imply the declaration of an object.

If that is the extent of what is discussed in the standard, then the short of
it is that the standard does not define what a variable is.
"An object is a region of storage. [Note: A function is not an object,
regardless of whether or not it occupies storage in the way that objects
do. ] An object is created by a definition (3.1), by a new-expression
(5.3.4) or by the implementation (12.2) when needed."

Seems like it isn't complete to me. Either the other ways (C-style?) to
instanciate an 'object' are either implied or just plain left out. Consider
the differences between:

int i; // created by a definition

int * j = new int; // by a new-expression

extern int k; // by the implementation

int * l = (int *)malloc(sizeof (int)); // what about me?
Nov 14 '05 #258
Julie wrote:
"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."

Fine -- but what that is saying is that when you create an object, you also
introduce a variable. The opposite is not necessarily true: introducing a
variable does *NOT* imply the declaration of an object.

No it says that you declare an object by using a variable. So when you
"declare" a variable in essence you declare an object.
If that is the extent of what is discussed in the standard, then the short of
it is that the standard does not define what a variable is.
It does. A variable (its name) denotes an object.
"An object is a region of storage. [Note: A function is not an object,
regardless of whether or not it occupies storage in the way that objects
do. ] An object is created by a definition (3.1), by a new-expression
(5.3.4) or by the implementation (12.2) when needed."

Seems like it isn't complete to me. Either the other ways (C-style?) to
instanciate an 'object' are either implied or just plain left out. Consider
the differences between:

int i; // created by a definition

int * j = new int; // by a new-expression

extern int k; // by the implementation

Actually defined in another compilation unit.
int * l = (int *)malloc(sizeof (int)); // what about me?

In the above cases we have:

int i;

i is a variable that denotes an object.
int *j=new int;
j is a variable that denotes an int * object, that is the pointer itself.

The new int that is created in the free store is another object, not
denoted by a variable, since it is not created by a variable declaration.

extern int k; means that it is defined elsewhere (in general) and the
"int i" case applies for the definition.
The malloc() situation is the same with the new int situation.

Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Nov 14 '05 #259
In comp.lang.c Ioannis Vranos <iv*@guesswh.at .grad.com> wrote:
Julie wrote:

extern int k; // by the implementation

Actually defined in another compilation unit.

Not necessarily. Consider the following, in the same translation unit.

static int a = 42;

extern int a; /* deceptive use of 'extern' */

--
Alex Monjushko (mo*******@hotm ail.com)
Nov 14 '05 #260

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