473,839 Members | 1,514 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 <vf************ *************** *****@4ax.com> Mark McIntyre <ma**********@s pamcop.net> writes:
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,

Accusing you of being an idiot is not exactly the same as accusing
"just about everyone else" of being an idiot.
yet refused to produce medical evidence to that effect? :-)

You produced yourself more than enough factual evidence. Thanks to
Google, it's here to stay ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #241
Da*****@cern.ch (Dan Pop) wrote:
In <i4************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern. ch (Dan Pop) wrote:
In <5b************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes: <much snippage>Take your own advice and learn to read for comprehension, Dan!

I do.

Obviously not.
Do you?

I do my very best.

Which is no guarantee of success.

Deliberately acting like the idiot you consider others to be won't
help you here. There is not much of a guarantee for anything in the
real world.
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.

Thanks, Dan, you presented just another evidence supporting my theory.
And BTW, mimicking paranoid behaviour doesn't help much, too.
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...

C'mon Dan, pretending you're an idiot two times in a single message is
far too much. Get real! My suggestion had nothing to do with hiding
something from someone, but everything with trying to move this OT
private discussion off the group, for the benefit of other readers.
You chose to ignore, so I choose to drop out.

Bye.
--
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 #242
In <gh************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern.c h (Dan Pop) wrote:

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

C'mon Dan, pretending you're an idiot two times in a single message is
far too much. Get real! My suggestion had nothing to do with hiding
something from someone, but everything with trying to move this OT
private discussion off the group, for the benefit of other readers.

Bogus argument. Those not interested have killfiled the thread long ago.
Neither you nor I have the slightest clue about who might be still
following this discussion and with what degree of interest.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #243
> 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;
}
Nov 14 '05 #244
"Michael" <mi************ @excite.com> wrote in message
news:20******** *************** ***@posting.goo gle.com...
Please describe (in code) a situation where two variables share the same memory location.

Dan you need to try stuff for yourself

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

Duck everybody!
Nov 14 '05 #245
Mabden wrote:
"Michael" <mi************ @excite.com> wrote in message
news:20******** *************** ***@posting.goo gle.com...
Please describe (in code) a situation where two variables share the same
memory
location.

Dan you need to try stuff for yourself

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

Duck everybody!

Eheheh (laughing behind a wall).
Nov 14 '05 #246
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.

Try again.
Nov 14 '05 #247
Julie <ju***@nospam.c om> writes:
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.

Try again.

No, of course nobody is saying htat &x == &p.

x and p are undeniably variables. The question is whether *p is also
a variable. If it is, the above program demonstrates a situation
where two variables share the same memory location; if it isn't, it
doesn't.

*p is certainly an object. The question of whether it's also a
"variable" is, in my opinion, a profoundly uninteresting one. The C
standard doesn't define "variable" as a technical term. It uses it
(informally) in a few places. It happens that it uses it only to refer
to declared objects, but we can't really infer anything from that.

--
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 #248
Keith Thompson wrote:

Julie <ju***@nospam.c om> writes:
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.

Try again.

No, of course nobody is saying htat &x == &p.

x and p are undeniably variables. The question is whether *p is also
a variable. If it is, the above program demonstrates a situation
where two variables share the same memory location; if it isn't, it
doesn't.

*p is certainly an object. The question of whether it's also a
"variable" is, in my opinion, a profoundly uninteresting one. The C
standard doesn't define "variable" as a technical term. It uses it
(informally) in a few places. It happens that it uses it only to refer
to declared objects, but we can't really infer anything from that.

*p _may_ be an object. Consider the following:

int *p = NULL;

*p is definitely not an object in this case.

Yes, profoundly uninteresting, but somewhat worthy of senseless never-ending
blather, of which I have been a major (unapologetic) contributor.

Since we are in comp.lang.c* newsgroups, the discussions are not strictly
limited to what the standard does/doesn't define -- such restricted topics are
discussed in comp.std.c*.

Using unions it is possible to declare a variable that shares the same
address. Dereferenced pointers (or references in C++) to a variable are at
most an alias to a variable, but not a variable in terms that I consider
well-defined. Others may (and obviously do, hopefully an informed view)
consider the term 'variable' differently.
Nov 14 '05 #249
Julie <ju***@nospam.c om> writes:
[...]
*p _may_ be an object. Consider the following:

int *p = NULL;

*p is definitely not an object in this case.
Right, but in the example we've been talking about, p has been
assigned the value &x (where x is a declared int object).
Yes, profoundly uninteresting, but somewhat worthy of senseless never-ending
blather, of which I have been a major (unapologetic) contributor.
As have I from time to time.
Since we are in comp.lang.c* newsgroups, the discussions are not
strictly limited to what the standard does/doesn't define -- such
restricted topics are discussed in comp.std.c*.

Actually, comp.lang.c tends to be about the language as defined by the
standard, whereas comp.std.c is about the standard itself. (I just
noticed that this is cross-posted to comp.lang.c++, which is almost
never a good idea.)

[...]

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

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