473,839 Members | 1,526 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 20382
In <9e************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern.c h (Dan Pop) wrote:
In <gs************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
<snip>
Agreed, but it's IMHO likely to confuse newbies, if e.g. someone calls
a dereferenced pointer a variable, as happened up-thread.

I disagree. It's perfectly intuitive (unless you happen to be Julie):
p points to a variable and *p is the variable pointed to by p. Why would
a newbie be confused by the simplest thing about pointers in C?

IMO we should agree to disagree: *p is not a proper identifier and it

Who sez it should be?
can't be initialized on declaration,
If it's not a proper identifier, you cannot declare it. But this doesn't
make it any less variable.
thus it's not a variable.
By *your* narrow minded definition of variable.
*p is
the contents of the memory location p points to, converted to the type
p is a pointer to.
Which is good enough to be called variable, unless the object in question
was declared as const.

BTW, a variable doesn't even need to have a name or to be declared.

int *p = malloc(sizeof *p);

creates an unnamed variable. You can access it with *p, just as you can
access a named variable whose address is stored in p.
<snip>
Definitly; it's much better to explain the "proper" terminology in
context. I consider this to be one (albeit not main) purpose of this
news-group.
When replying to a newbie question, you must consider the newbie's
priorities. Giving him a full lesson about the "proper" terminology
is not exactly what I call helpful.

Is your world really black and white only?

Non sequitur. See below.
There's not a great void
between a simple explanation and an exhaustive lecture in applied
Standardese. A comprehensible explanation, enriched with some
for that matter) is what I'd call not only helpful, but educative,
too.

Not if the poster has a completely different set of priorities, i.e. he is
already struggling to understand the concepts from his C book, using the
author's terminology and the last thing he needs is more new concepts and
definitions. Maybe you have forgotten those days, but I haven't (heck,
it's a mere 16 years ago).
There is plenty of time to learn
the "proper" terminology later and most people have nothing to lose by
never learning it. Again, real world vs the ivory tower: I've been
programming for years without the slightest clue about the "proper"
terminology and the code written back then is still working...

You're not the only one. But, again, what's wrong with learning
something correct every once in a while, even if it's not highly
important for the practical solution of the problem at hand?

See above. The newbie is already learning a lot of things in a short
time, so your "once in a while" is strongly misplaced. When the OP is not
a newbie (it's quite easy to tell the difference, even if he doesn't
mention), correcting improper terminology is a must. As I said, it's a
matter of getting the priorities right. So where does my black and white
world comes from?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #231
Da*****@cern.ch (Dan Pop) wrote:
In <9e************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
<snippage>
[...] *p is not a proper identifier and it
can't be initialized on declaration,
thus it's not a variable.

By *your* narrow minded definition of variable.

I'd call it sensible. <OT> And, BTW, it matches the definition in the
C++ standard, a language distinct from C, but closely related. </OT>

<snip>BTW, a variable doesn't even need to have a name or to be declared.

int *p = malloc(sizeof *p);

creates an unnamed variable. You can access it with *p, just as you can
access a named variable whose address is stored in p.
By your logic, given char *p = "abc"; *p constitutes a variable of
type char.

<snip>Not if the poster has a completely different set of priorities, i.e. he is
already struggling to understand the concepts from his C book, using the
author's terminology and the last thing he needs is more new concepts and
definitions. Maybe you have forgotten those days, but I haven't (heck,
it's a mere 16 years ago).
I certainly didn't (coincidentally , it's only about 16 years ago for
me, too), but I was always grateful when my local C guru corrected my
flawed mental image of basic concepts and terminology, as soon as
possible. YMMV.

<snip> When the OP is not
a newbie (it's quite easy to tell the difference, even if he doesn't
mention), correcting improper terminology is a must. As I said, it's a
matter of getting the priorities right.
I cannot see the benefit of first teaching sloppiness to newbies and
then give them a hard time to get it correct later. Again, YMMV.
So where does my black and white
world comes from?

Since you're the only one able to provide a sensible answer to this
question, how am I supposed to know?

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #232
In <5n************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern.c h (Dan Pop) wrote:
In <9e************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
<snippage>
[...] *p is not a proper identifier and it
can't be initialized on declaration,
thus it's not a variable.

By *your* narrow minded definition of variable.

I'd call it sensible. <OT> And, BTW, it matches the definition in the
C++ standard, a language distinct from C, but closely related. </OT>

If you believe that standards are the right source of definitions for
terms used outside of those standards context, I invite you to read
the C standard definition of the term "byte". Use it in a non-C
context and people will laugh at you.

The definition from the C++ standard is predating languages supporting
pointers. In good old FORTRAN, you needed a name to access a variable.
<snip>
BTW, a variable doesn't even need to have a name or to be declared.

int *p = malloc(sizeof *p);

creates an unnamed variable. You can access it with *p, just as you can
access a named variable whose address is stored in p.
By your logic, given char *p = "abc"; *p constitutes a variable of
type char.

Nope. p doesn't point to a modifiable object, does it? So, how can you
invoke *my* logic?

Translated in the C lingo, a variable is a modifiable object whose
address is known. So, memory leaks, const objects and string literals
don't count.
<snip>
Not if the poster has a completely different set of priorities, i.e. he is
already struggling to understand the concepts from his C book, using the
author's terminology and the last thing he needs is more new concepts and
definitions . Maybe you have forgotten those days, but I haven't (heck,
it's a mere 16 years ago).
I certainly didn't (coincidentally , it's only about 16 years ago for
me, too), but I was always grateful when my local C guru corrected my
flawed mental image of basic concepts and terminology, as soon as
possible. YMMV.

No need to muddle the issue: we're NOT talking here about flawed mental
images of basic concepts, the discussion is strictly limited to
terminology issues. No one was arguing about preserving the newbies'

And my mileage did vary, because I was self taught and was the only one
of VAX C as my one and only reference documentation (in addition to my
handwritten copy of Appendix A of K&R1).
<snip>
When the OP is not
a newbie (it's quite easy to tell the difference, even if he doesn't
mention), correcting improper terminology is a must. As I said, it's a
matter of getting the priorities right.

I cannot see the benefit of first teaching sloppiness to newbies and
then give them a hard time to get it correct later. Again, YMMV.

Is K&R2 teaching sloppiness to newbies, by not using the C standard
terminology? The term "variable" is intensively used by Kernighan, in the
tutorial part. And, horror of horrors, he's also talking about operator
precedence and associativity, which is sheer heresy in the eyes of the
anal c.l.c regular. OTOH, good luck trying to find any mention of
undefined behaviour...

Although you may be unable to get it, the standard terminology is not
needed to teach C programming correctly. Even the C standard is
occasionally using the term "variable":

10 EXAMPLE 2 In executing the fragment

char c1, c2;
/* ... */
c1 = c1 + c2;

the ``integer promotions'' require that the abstract machine
promote the value of each variable to int size and then add the
two ints and truncate the sum.
....
41) Thus, an automatic variable can be initialized to a trap
representation without causing undefined behavior, but the
value of the variable cannot be used until a proper value
is stored in it.
....
equal. Therefore, for full portability, the variable c should
be declared as int.
....
If clause-1
is a declaration, the scope of any variables it declares is the
remainder of the declaration and the entire loop, including the
other two expressions;
....
133) Thus, clause-1 specifies initialization for the loop,
possibly declaring one or more variables for use in the
loop;
....
164) For a variable z of complex type, z == creal(z) + cimag(z)*I.
....
A floating-point status flag is a
system variable whose value is set (but never cleared) when a
floating-point exception is raised, which occurs as a side effect
of exceptional floating-point arithmetic to provide auxiliary
information. A floating-point control mode is a system variable
whose value may be set by the user to affect the subsequent
behavior of floating-point arithmetic.
So where does my black and white world comes from?

Since you're the only one able to provide a sensible answer to this
question, how am I supposed to know?

You made the accusation, you have to support it! Or apologise, if unable
to support it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #233
Da*****@cern.ch (Dan Pop) wrote:
In <5n************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern. ch (Dan Pop) wrote:
<much snippage>
Although you may be unable to get it, the standard terminology is not
needed to teach C programming correctly. Even the C standard is
occasionally using the term "variable":
[Occurrences of the term "variable" in the standard snipped.]

I did the search myself some time ago, and, as I already mentioned
elsethread, the term "variable" in the standard is used solely to
describe named objects. Thanks for making my point.
So where does my black and white world comes from?

Since you're the only one able to provide a sensible answer to this
question, how am I supposed to know?

You made the accusation, you have to support it!

How is the question "Is your world really black and white only?" to
comprehension, Dan!
Or apologise, if unable to support it.

No accusation - no apology. Oh, and on a more general note: I suggest
you cease to request politeness from others, up until your own social
skills conform to at least stone-age standards; thank you.

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #234
In <5b************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern.c h (Dan Pop) wrote:
In <5n************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern .ch (Dan Pop) wrote:
<much snippage>
Although you may be unable to get it, the standard terminology is not
needed to teach C programming correctly. Even the C standard is
occasionall y using the term "variable":
[Occurrences of the term "variable" in the standard snipped.]

I did the search myself some time ago, and, as I already mentioned
elsethread, the term "variable" in the standard is used solely to
describe named objects. Thanks for making my point.

How many examples in the standard deal with unnamed objects? Are you sure
your point has any validity at all?
So where does my black and white world comes from?

Since you're the only one able to provide a sensible answer to this
question, how am I supposed to know?

You made the accusation, you have to support it!

How is the question "Is your world really black and white only?" to
be taken as accusation?

I've never seen it used it as a compliment, so please enlighten me.

I do. Do you?
Or apologise, if unable to support it.

No accusation - no apology. Oh, and on a more general note: I suggest
you cease to request politeness from others, up until your own social
skills conform to at least stone-age standards; thank you.

Whenever I make an accusation, I'm ready to support it rather than
cowardly pretending that it wasn't an accusation in the first place.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #235
Irrwahn Grausewitz <ir*******@free net.de> writes:
[...]
[Occurrences of the term "variable" in the standard snipped.]

I did the search myself some time ago, and, as I already mentioned
elsethread, the term "variable" in the standard is used solely to
describe named objects. Thanks for making my point.

There are few enough uses of the term "variable" (as a noun) in the
standard that I don't think we can draw any conclusions about the
precise definition. IMHO, there's not much of a point to be made one
way or the other.

--
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 #236
Da*****@cern.ch (Dan Pop) wrote:
In <5b************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern. ch (Dan Pop) wrote:
In <5n************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cer n.ch (Dan Pop) wrote:
<much snippage>
So where does my black and white world comes from?

Since you're the only one able to provide a sensible answer to this
question, how am I supposed to know?

You made the accusation, you have to support it!
How is the question "Is your world really black and white only?" to
be taken as accusation?

I've never seen it used it as a compliment, so please enlighten me.

If you actually think that every assertion or question that is not a
compliment automatically constitutes an accusation, you know the
reason why I felt compelled to ask the question (sic!) in the first
place. And you answered it affirmative in the same sentence. Nice
job.

I do.

Obviously not.
Do you?

I do my very best.
No accusation - no apology. Oh, and on a more general note: I suggest
you cease to request politeness from others, up until your own social
skills conform to at least stone-age standards; thank you.

Whenever I make an accusation, I'm ready to support it rather than
cowardly pretending that it wasn't an accusation in the first place.

No place for cowardice, since it wasn't an accusation, despite your
obvious lack of ability to differentiate in nuances, rather than
coercing each and every thing in some kind of crude binary system.

However, you showed enough evidence that your world in fact *is* black
and white only. This evidence is to be found above, as well as in
literally thousands of abusenet messages you posted. You know how to
open your out-box, or go ogle, don't you?

If you want to continue this discussion, feel free to send me a
private email.

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #237
On Mon, 12 Jul 2004 18:33:25 +0200, in comp.lang.c , Irrwahn Grausewitz
<ir*******@free net.de> wrote:
Da*****@cern.c h (Dan Pop) wrote:
Whenever I make an accusation, I'm ready to support it rather than

Yeah? Like when Dan accused just about everyone else of being an idiot, yet
refused to produce medical evidence to that effect? :-)

Get real.
No place for cowardice, since it wasn't an accusation, despite your
obvious lack of ability to differentiate in nuances, rather than
coercing each and every thing in some kind of crude binary system.

Irrwahn, you're shouting down a well.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #238
Mark McIntyre <ma**********@s pamcop.net> wrote:
On Mon, 12 Jul 2004 18:33:25 +0200, in comp.lang.c , Irrwahn Grausewitz
<ir*******@fre enet.de> wrote:
Da*****@cern. ch (Dan Pop) wrote:
Whenever I make an accusation, I'm ready to support it rather than

Yeah? Like when Dan accused just about everyone else of being an idiot, yet
refused to produce medical evidence to that effect? :-)

Get real.
No place for cowardice, since it wasn't an accusation, despite your
obvious lack of ability to differentiate in nuances, rather than
coercing each and every thing in some kind of crude binary system.

Irrwahn, you're shouting down a well.

Yup, I know, nice ECHO... echo... e.o... o... o... ;-)

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #239
In <i4************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern.c h (Dan Pop) wrote:
In <5b************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern .ch (Dan Pop) wrote:
In <5n************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
>Da*****@ce rn.ch (Dan Pop) wrote:
<much snippage>
So where does my black and white world comes from?
>
>Since you're the only one able to provide a sensible answer to this
>question , how am I supposed to know?

You made the accusation, you have to support it!

How is the question "Is your world really black and white only?" to
be taken as accusation?
I've never seen it used it as a compliment, so please enlighten me.

If you actually think that every assertion or question that is not a
compliment automatically constitutes an accusation, you know the
reason why I felt compelled to ask the question (sic!) in the first
place. And you answered it affirmative in the same sentence. Nice
job.

You missed the irony behind my statement, confirming the cliche about
the Germans' sense of humour (or lack thereof).

I do.

Obviously not.
Do you?

I do my very best.

Which is no guarantee of success.
No accusation - no apology. Oh, and on a more general note: I suggest
you cease to request politeness from others, up until your own social
skills conform to at least stone-age standards; thank you.

Whenever I make an accusation, I'm ready to support it rather than
cowardly pretending that it wasn't an accusation in the first place.

No place for cowardice, since it wasn't an accusation, despite your
obvious lack of ability to differentiate in nuances, rather than
coercing each and every thing in some kind of crude binary system.

On the contrary, I'm quite good at differentiating nuances, therefore
I didn't miss the pejorative one behind your "neutral" question.
If you want to continue this discussion, feel free to send me a
private email.

Why should I bother? I have nothing to hide from anyone...

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

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