473,838 Members | 1,694 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 20374
>> In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes: [snippage -- but "a" and "b" are distinct objects of type "int"]
printf("address es are %s\n", &a == &b ? "equal" : "unequal");
... If [this] prints
a = 10, b = 20, addresses are equal
does it imply that the compiler is non-conforming?
Chris Torek <no****@torek.n et> writes:
In C at least, yes it does. (I would be quite surprised if the

In article <news:ln******* *****@nuthaus.m ib.org>
Keith Thompson <ks***@mib.or g> writes:I believe you, but how does the standard imply this?

The most important item is the description of the "!=" operator.
This is from a C99 draft, but the key wording is unchanged from
C89:

6.3.9 Equality operators
...
Semantics
...
[#4] If two pointers to object or incomplete types are both
null pointers, they compare equal. If two pointers to
object or incomplete types compare equal, they both are null
pointers, or both point to the same object, or both point
one past the last element of the same array object.79 ...

One also must look at the unary-& operator, of course; but if &a == &b,
the two pointers involved are:

- both NULL (better not be), or

- both point to the same object (better not; "a" and "b" are not
the same object, even if they occupy the same physical location
*after* optimization), or

- both point one past the last element of the same array object.

Since none of these is true, the result of the "==" and "!="
operators must be "not equal".

I think the stickiest point here really lies in figuring out what
defines a "unique" or "distinct" object. Other than via unions,
or nonportable assembly tricks like __asm__(".globa l a, b; b = a"),
objects with different "ordinary variable names" must be distinct,
although the inverse is not true (same-name can still be distinct):

int a, b, c; /* a, b, and c are all distinct */
void f(void) {
int a;
extern int b;
static char *c;
/* f()'s a and c are not the same as the file-scope a and c,
while f()'s b is the same as the file-scope b. */
...
}

Unions make a mockery of distinctness:

union {
char a;
float b;
long double complex c; /* c99 only */
} u;

Here u.a, u.b, and u.c are not actually distinct objects, although
sizeof(u.a) is 1, and probably sizeof(u.b) is greater than 1
(typically 4) while sizeof(u.c) is probably greater still (typically
16, 24, or 32 today), so that "most of" u.c is probably distinct
from u.a, in a way.

One can also make a mess with macros, but in general, if you see
C code that refers to "i" and then "j", these will be distinct
objects. Even more complicated names/expressions like s.x and s.y
or p->x and p->y *probably* refer to distinct objects.

The rules for whether identical ordinary identifiers refer to the
same underlying object *are* spelled out (in great detail) in the
Standard, but this requires going through the sections on scope
and linkage. Note that there are cases where "logically distinct"
objects *are* allowed to share underlying memory, but the only
way to test it is to invoke undefined behavior:

void f(void) {
int *p;
{
int i;
p = &i;
}
/* BEWARE: the value in p is now "indetermin ate" */
{
int j;
printf("i (which no longer exists) and j do%s occupy "
"the same space\n", (p == &j) ? "" : " not");
/* ERROR -- use of the indeterminate value in p produces
undefined behavior */
}
}

This function will generally compile and run (perhaps with a warning
from a particularly clever compiler) but produces undefined behavior,
so one cannot prove anything from it. Your (Keith's) original
example *does* have defined behavior, and must claim that the
--
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 #271
Chris Torek <no****@torek.n et> writes:
In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes: [snippage -- but "a" and "b" are distinct objects of type "int"] printf("address es are %s\n", &a == &b ? "equal" : "unequal");
... If [this] prints
a = 10, b = 20, addresses are equal
does it imply that the compiler is non-conforming?

Chris Torek <no****@torek.n et> writes:
In C at least, yes it does. (I would be quite surprised if the

In article <news:ln******* *****@nuthaus.m ib.org>
Keith Thompson <ks***@mib.or g> writes:
I believe you, but how does the standard imply this?

The most important item is the description of the "!=" operator.
This is from a C99 draft, but the key wording is unchanged from
C89:

6.3.9 Equality operators
...
Semantics
...
[#4] If two pointers to object or incomplete types are both
null pointers, they compare equal. If two pointers to
object or incomplete types compare equal, they both are null
pointers, or both point to the same object, or both point
one past the last element of the same array object.79 ...

[snip]

Yes, that does it. Thanks.

--
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 #272

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