473,839 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
please Help
Wiating for reply.
Nov 14 '05
271 20376
In <cr************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
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.


How would you call *p, though? It has the semantics of a variable,
doesn't it?
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.


That's something too subtle for Julie to understand. She seems to be
blind to the semantic difference between p (that can point to a variable)
and *p (that *is* the variable pointed to by p).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #221
In <gs************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Da*****@cern.c h (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
connectio ns 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.


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?
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.


When replying to a newbie question, you must consider the newbie's
priorities. Giving him a full lesson about the "proper" terminology
instead of providing a simple explanation in terms he already understands
is not exactly what I call helpful. 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...
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... ;-)


Then, why are you arguing on the ivory tower side? ;-)

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

Julie wrote:

pete wrote:

Julie wrote:

> Please describe (in code) a situation where two
> variables share the same memory location.

In post
http://groups.google.com/groups?selm...mindspring.com
*node is a variable.
In the code example, *node has the same address as either
*tail or *head.


I meant:

"In the code example, *node has the same address as either
tail or head."

--
pete
Nov 14 '05 #223
Irrwahn Grausewitz wrote:

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.


In context specific conditions (such as the code above), this is true where p
points to a.

However (and it wasn't clear in my response), generally speaking: an arbitrary
point x *may* point to a variable, but not necessarily.
Nov 14 '05 #224
Dan Pop wrote:

In <cr************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
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.


How would you call *p, though? It has the semantics of a variable,
doesn't it?
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.


That's something too subtle for Julie to understand. She seems to be
blind to the semantic difference between p (that can point to a variable)
and *p (that *is* the variable pointed to by p).


Dan -- your condescending attitude benefits no one in this newsgroup, and only
selfishly serves you.

I was speaking in general terms, not specifically. In the case of the code
posted, *p does point to a variable a, but only in this specific case. In
general terms, a pointer x *may* point to a variable.

Here are some examples of pointers that do *not* point to variables:

int * x = NULL;

const int b = 123;
const int * w = &b;

int * r = &printer_port_o ut;
Nov 14 '05 #225

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
Dan Pop wrote:

In <cr************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
Julie <ju***@nospam.c om> wrote:

That's something too subtle for Julie to understand. She seems to be blind to the semantic difference between p (that can point to a variable) and *p (that *is* the variable pointed to by p).
Dan -- your condescending attitude benefits no one in this

newsgroup, and only selfishly serves you.


It doesn't serve him very well. ;-)

Jonathan
Nov 14 '05 #226
Dan Pop wrote:
That's something too subtle for Julie to understand. She seems to be
blind to the semantic difference between p (that can point to a variable)
and *p (that *is* the variable pointed to by p).

Well, you and Julie actually use the term variable in the place of the
term object.
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,

Language Lawyer :-)
Nov 14 '05 #227
Julie wrote:

Dan Pop wrote:

In <cr************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
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'd call p a variable, but not *p.

I agree with you here.


How would you call *p, though? It has the semantics of a variable,
doesn't it?
>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.


That's something too subtle for Julie to understand.
She seems to be
blind to the semantic difference between p
(that can point to a variable)
and *p (that *is* the variable pointed to by p).


Dan -- your condescending attitude
benefits no one in this newsgroup, and only
selfishly serves you.

I was speaking in general terms, not specifically.
In the case of the code posted,
*p does point to a variable a,


No it doesn't.
*p is an object of type int. It doesn't point anywhere.

--
pete
Nov 14 '05 #228
Da*****@cern.ch (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
can't be initialized on declaration, thus it's not a variable. *p is
the contents of the memory location p points to, converted to the type
p is a pointer to.

<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
instead of providing a simple explanation in terms he already understands
is not exactly what I call helpful.


Is your world really black and white only? There's not a great void
between a simple explanation and an exhaustive lecture in applied
Standardese. A comprehensible explanation, enriched with some
additional remarks about terminology (or other theoretical concepts,
for that matter) is what I'd call not only helpful, but educative,
too.
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?

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


Then, why are you arguing on the ivory tower side? ;-)


Since I'm not, the only correct answer to this question is: Mu. ;-)

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 #229
Da*****@cern.ch (Dan Pop) wrote:
In <cr************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
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.


How would you call *p, though? It has the semantics of a variable,
doesn't it?


But only to some degree, and only in this special case, where p
happens to point to an object created by declaration of a variable,
that coincidentally has the same type p is a pointer to. See also
my reply in the other (non-crossposted) sub-thread, as well as
Ioannis' reply <cc***********@ ulysses.noc.ntu a.gr>, with which I
wholeheartedly agree.

<snip>
--
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 #230

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

Similar topics

270
3990
by: Jatinder | last post by:
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.
12
8490
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now I really want to get back into it as I see a point with GNU/Linux. I want to get my old skills back and write something or help on some projects etc. I need some good books. I used to have one called "A Book On C", but sold it,
11
2230
by: John Salerno | last post by:
Similar to the Python Challenge, does anyone know of any other websites or books that have programming puzzles to solve? I found a book called "Puzzles for Hackers", but it seems like it might be a little advanced for me, and I've also read that it focuses too much on encryption and security issues and doesn't really have coding problems, exactly. But something like that book would be fun to have.
1
13120
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this the program look like this import java.util.ArrayList; import java.util.Random;
0
2031
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this this run the random words for the program import javax.swing.JOptionPane; import java.util.ArrayList; import java.util.Random; public class CrossWordPuzzleTester {
44
3577
by: Jon Harrop | last post by:
Microsoft Research are developing a functional programming language called F# for .NET and I've been playing with it recently. I've uploaded some demos here: http://www.ffconsultancy.com/dotnet/fsharp/ I'm keen to see what Windows developers think of this language as we're considering using it to develop commercial applications on the Windows platform.
5
4477
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do is to solve jigsaw puzzles using the computer. The jigsaw puzzle here is a square of dimension d (a puzzle with d^2 pieces) and the jigsaw pieces (all same dimensions) are of dimensions H x W (Which means the pieces have ‘H’ rows of ‘W’...
3
3213
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up to bottom (S), or (iv) bottom to up (N). The program will print the starting place and the direction of each word. Limitations The number of words to be searched can be at most 100, the size of the puzzle N can be minimum 5 maximum 20....
4
20015
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to get the tiles in order, 1 through 15, from left to right, top to bottom, by just sliding tiles into the empty square. In this configuration, the goal would be to get the 14 and 15 to switch places, without affecting any of the other squares. Your...
13
3130
by: btkuhn | last post by:
Hi guys, I'm learning Python by teaching myself, and after going through several tutorials I feel like I've learned the basics. Since I'm not taking a class or anything, I've been doing challenges/programs to reinforce the material and improve my skills. I started out with stuff like "Guess my number" games, hangman, etc. and moved on to making poker and card games to work with classes. For GUIs I created games like minesweeper, and a...
0
9854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10290
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7827
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7015
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5681
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.