473,854 Members | 1,665 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 20406
In <of************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern.c h (Dan Pop) wrote:
In <jk************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
1. Technically, the discussion is about objects, identifiers that ^^^^^^^^^^^ designate/denote objects (aka variable names in C++), and values.

Nope, the discussion was about "variables" .

Then, pray tell, how is the term "variable" defined, WRT to the C
language? Those who were using it were actually talking about named
objects, *technically*. However, except for C++, the term variable
isn't officially defined to mean "named object".
As long as c.l.c is still an imperfect ivory tower (i.e. it still has
connections with the real world) it is highly unrealistic to ignore
popular CS jargon terms like "variable" and "global", simply because the
C standard itself doesn't use them.

It's indeed highly unrealistic to expect everyone to avoid those
terms. However, a quick look in the archives shows that the use of
correct, well-defined terms, instead of sloppy jargon, serves well to
reduce the risk of confusion. What's wrong with object, external
linkage, file scope, etc.pp., after all?

Nothing, except that answering a newbie question in a jargon that is
completely unknown to him is not going to *really* help him. Neither is
pointing him to the standard, so that he learns the "proper" terminology.

If you really want to help a newbie, you *must* use the terminology he
understands. It's as simple as that, hence my reference to the perfect
ivory tower.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #211
In <yD************ *****@newssvr27 .news.prodigy.c om> "Mabden" <mabden@sbc_glo bal.net> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cc******* ***@sunnews.cer n.ch...
In <nV************ *******@newssvr 29.news.prodigy .com> "Mabden"<mabden@sbc_gl obal.net> writes:
>Q. "What will happen if I compile and run this program??"
>A. "You'll get a warning saying main() has no return value."
>
>Do you see a question about standards or committees? No, just "what will
>happen". So I showed a specific case of what would happen. Others have
>reported their results of what happened.

The OP question was generic! He didn't ask "what will happen if *you*
compile and run this program?", did he? You can't provide an equally
must be based on the C language definition.

But, Dan, don't we want to teach these newcomers to code properly? Shouldn't
one point out that a function declared as returning an int, that does not
return an int, may have a flaw?

Yup, most definitely, but not by making patently false statements.
Ask yourself if my short post (one line) helped to show this general error
that may have deep implications if ignored in large, industrial
applications .
I think my post was consise, and germain, without being overbearing; and
therefore unheard.

Your post was technically incorrect and therefore, unsuitable for
technical newsgroups, period.
Furthermore, the specific behaviour of one compiler or another is usually
considered off topic in this newsgroup, you need a *good* reason for

"Brevity is the soul of wit". One can teach without a stick in hand, Dan.

Blatantly false statements are the worst way of teaching. Imagine that
the OP doesn't get any warning, which is quite likely (e.g. gcc doesn't
warn by default). What is he supposed to understand from your post?
Are you helping *or* confusing the OP?

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

Julie <ju***@nospam.c om> wrote:
Irrwahn Grausewitz wrote:

Julie <ju***@nospam.c om> wrote:
<much snippage>

>I'm all for using correct terms. What would you call 'a' here:
>
>int main()
>{
> int a = 1;
> return 0;
>}

Technically correct:
'a' is an identifier designating an object of type int.

Jargon (and in this case I think there's no place for confusion):
'a' is a variable of type int.

That was easy ;-). Now for the next round, you first:
What would you call p and *p, respectively?

int main(void)
{
int a = 1;
int *p = &a;
*p = 2;
return 0;
}

I call 'p' a "pointer [to an int]" and '*p' "p dereferenced" or "a pointer [to
an int] dereferenced".

So you think the term "variable" is applicable to neither p nor *p?

I'd call p a variable, but not *p. I'd say that *p might /point/ to a
variable, but that is it.
Nov 14 '05 #213
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cc******** **@sunnews.cern .ch...
Are you helping *or* confusing the OP?

<sigh> Yes Dan, you are surely right, as always. I will try to refrain for
posting, as we all should. Only Dan Pop has the right answers. When will we
all learn to shut up and let You speak Your word to enlighten us all. I'm
not worthy, I'm not worthy...

--
Mabden
Nov 14 '05 #214
In article <1j************ *****@newssvr25 .news.prodigy.c om>,
mabden@sbc_glob al.net says...
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cc******** **@sunnews.cern .ch...
Are you helping *or* confusing the OP?

<sigh> Yes Dan, you are surely right, as always. I will try to refrain for
posting, as we all should. Only Dan Pop has the right answers. When will we
all learn to shut up and let You speak Your word to enlighten us all. I'm
not worthy, I'm not worthy...

At last the grasshopper finally understands Dan.

:-)

Nov 14 '05 #215
Julie <ju***@nospam.c om> wrote:
Irrwahn Grausewitz wrote:
Julie <ju***@nospam.c om> wrote:
>Irrwahn Grausewitz wrote: <snip> >> What would you call p and *p, respectively?
>>
>> int main(void)
>> {
>> int a = 1;
>> int *p = &a;
>> *p = 2;
>> return 0;
>> }
>
>I call 'p' a "pointer [to an int]" and '*p' "p dereferenced" or "a pointer [to
>an int] dereferenced".
So you think the term "variable" is applicable to neither p nor *p?

I'd call p a variable, but not *p.

I agree with you here.
I'd say that *p might /point/ to a
variable, but that is it.

I beg to differ, I'd rather say that p points to a variable.

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 #216
"Rufus V. Smith" <no****@nospam. com> wrote:
"Irrwahn Grausewitz" <ir*******@free net.de> wrote in message
news:il******* *************** **********@4ax. com...
[cross-post to clc++ deliberately stripped]

Keith Thompson <ks***@mib.or g> wrote:

>Whether const-qualified objects are variables is a
>question I won't try to answer.

Indeed; "read-only variable" looks too much like an oxymoron to me.

In the embedded world, there are many non-const variables we
someone else writes them.

Right; I should've written: the term "constant variable" looks like
an oxymoron.

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 #217
Da*****@cern.ch (Dan Pop) wrote:
Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern. ch (Dan Pop) wrote: <snip>
As long as c.l.c is still an imperfect ivory tower (i.e. it still has
connection s with the real world) it is highly unrealistic to ignore
popular CS jargon terms like "variable" and "global", simply because the
C standard itself doesn't use them.
It's indeed highly unrealistic to expect everyone to avoid those
terms. However, a quick look in the archives shows that the use of
correct, well-defined terms, instead of sloppy jargon, serves well to
reduce the risk of confusion. What's wrong with object, external
linkage, file scope, etc.pp., after all?

Nothing, except that answering a newbie question in a jargon that is
completely unknown to him is not going to *really* help him.

Agreed, but it's IMHO likely to confuse newbies, if e.g. someone calls
a dereferenced pointer a variable, as happened up-thread.
Neither is
pointing him to the standard, so that he learns the "proper" terminology.
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.
If you really want to help a newbie, you *must* use the terminology he
understands. It's as simple as that, hence my reference to the perfect
ivory tower.

Fortunately, c.l.c will never become a perfect ivory tower, or at
the very least as long as I continue to post here... ;-)

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 #218
Julie wrote:

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?

No, I don't, and don't call me Shurly.

For
int *pointer = &b;

*pointer has the same address as b.
*pointer is a variable.

not the _value_ of variables.

*node is a variable.
In the code example, *node has the same address as either

--
pete
Nov 14 '05 #219
In <1j************ *****@newssvr25 .news.prodigy.c om> "Mabden" <mabden@sbc_glo bal.net> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cc******* ***@sunnews.cer n.ch...
Are you helping *or* confusing the OP?
<sigh> Yes Dan, you are surely right, as always.

I am right when I am right and wrong when I am wrong. I am neither
always right nor always wrong.
I will try to refrain for posting, as we all should.
When not having the correct answer.
Only Dan Pop has the right answers. When will we
all learn to shut up and let You speak Your word to enlighten us all. I'm
not worthy, I'm not worthy...

Cut the crap!

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

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