473,854 Members | 1,584 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 20410
In <YX************ *******@newssvr 27.news.prodigy .com> "Mabden" <mabden@sbc_glo bal.net> writes:
"Michael" <mi************ @excite.com> wrote in message
news:20******* *************** ****@posting.go ogle.com...
> Please describe (in code) a situation where two variables share the samememory > location.
>

Dan you need to try stuff for yourself


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


Nope, he isn't, but you're way too stupid to realise that. Clue: he was
replying to a request made by Julie and incorrectly mentioning my name.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #251
In comp.lang.c Julie <ju***@nospam.c om> wrote:
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.


That is not what he is saying. You are failing to grasp that *p is a
variable. Perhaps this quote from page 45 of K&R2 will enlighten you:

"...The increment and decrement operators can only be applied
to variables..."

This, in my mind, would mean that *p can be considered a variable, at
least part of the time. Technically, of course, *p can hardly be
considered a variable when p does not contain the address of a valid
object.

--
Alex Monjushko (mo*******@hotm ail.com)
Nov 14 '05 #252
I haven't read this thread, but:
You have two variables: x and p. Are you saying that &x == &p? I sure hope
not.


Of course, you can easily construct that sort of aliasing:

int a, &b = a;
assert( &a == &b );

I suspect the point of the thread was something else.

Also, FWIW, optimizers routinely fold objects like a and b below (i.e.,
they can occupy the same memory address) because the optimizer can prove
that a conforming program can't tell because their uses don't overlap:

#include <stdio.h>

int main() {
int a = 10;
printf( "%d", a );
int b = 20;
printf( "%d", b );
}

Herb

---
Herb Sutter (www.gotw.ca)

Convener, ISO WG21 (C++ standards committee) (www.gotw.ca/iso)
Contributing editor, C/C++ Users Journal (www.gotw.ca/cuj)
Visual C++ architect, Microsoft (www.gotw.ca/microsoft)
Nov 14 '05 #253
Herb Sutter <hs*****@gotw.c a> writes:
[...]
Also, FWIW, optimizers routinely fold objects like a and b below (i.e.,
they can occupy the same memory address) because the optimizer can prove
that a conforming program can't tell because their uses don't overlap:

#include <stdio.h>

int main() {
int a = 10;
printf( "%d", a );
int b = 20;
printf( "%d", b );
}


To expand on that point a bit, an optimizer can fold a and b because
it can prove that *this particular* program can't tell the difference
(and it's allowed to assume, for purposes of its proof, that the
program doesn't exhibit undefined behavior).

A conforming program could compare the addresses of a and b and expect
them to be different, but that would be a different program, and one
in which the optimizer wouldn't be allowed to fold a and b.

Actually, I'm making an assumption that I'm not 100% certain is
correct. Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("address es are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?

This is cross-posted to comp.lang.c and comp.lang.c++. If you're
going to reply based on either language standard, please follow up to
the appropriate group.

--
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 #254
Keith Thompson wrote:
[...]

Fellows we got in the same stuff again. Actually the wrong side of the
subject has been erased. At first cross-posting in clc++ and clc has
nothing useful to provide. They are separate languages. Regarding C++ as
I had said in some messages earlier in the thread:

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

http://www23.brinkster.com/noicys
Nov 14 '05 #255
In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes:
... Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("address es are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?


In C at least, yes it does. (I would be quite surprised if the
answer changes for C++.) Note that the compiler *can* still combine
the underlying storage space for "a" and "b", as long as it *also*
pretends that &a != &b even though then &a == &b. For instance,
suppose the compiler first tries to optimize the printf() call.
At this time, "a" and "b" are still separate entities in the
compiler's internal representation (e.g., separate RTL pseudo-registers
inside GCC). Thus the result of &a==&b is a known constant 0 (in
C). The next step is to replace:

printf("address es are %s\n", 0 ? "equal" : "unequal");

with:

printf("address es are %s\n", "unequal");

and perhaps even optimize this into:

puts("addresses are unequal"); /* note, puts() adds the \n */

Now that this has been done, there is no longer any occurrence of
&a or &b, so now the entities "a" and "b" can be folded together
to occupy the same hard-register-or-stack-slot. So now &a==&b even
though the code that prints whether &a==&b says they are not!
(Of course, if they are in a "hard register" they may not have an
address at all; and in the case of GCC, at least, constant propagation
would result in their removal entirely, at this point.)

If you were to replace the above printf() call with:

printf("address es are %p and %p\n", (void *)&a, (void *)&b);

the system would have to print two different numbers, in general,
and this would probably preclude the sort of folding described
here.
--
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
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #256
Chris Torek <no****@torek.n et> writes:
In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes:
... Can a conforming implementation fold a and b if their uses
don't overlap, but the program examines their addresses? In other
words, the following program:

#include <stdio.h>
int main(void)
{
int a, b;

a = 10;
printf("a = %d, ", a);

b = 20;
printf("b = %d, ", b);

printf("address es are %s\n", &a == &b ? "equal" : "unequal");

return 0;
}

should normally print

a = 10, b = 20, addresses are unequal

if a and b are not folded. If it prints

a = 10, b = 20, addresses are equal

does it imply that the compiler is non-conforming?


In C at least, yes it does. (I would be quite surprised if the
answer changes for C++.)


I believe you, but how does the standard imply this?

--
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 #257
Ioannis Vranos wrote:

Keith Thompson wrote:
> [...]
Fellows we got in the same stuff again. Actually the wrong side of the
subject has been erased. At first cross-posting in clc++ and clc has
nothing useful to provide. They are separate languages. Regarding C++ as
I had said in some messages earlier in the thread:

The C++ standard says:


Fortunately, comp.lang.c* doesn't have to restrict itself to just specifically
what is contained/defined within the standard. Such limited discussions are
appropriate in comp.std.c*; here we can talk about the language, as defined by
the standard.
"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."
Fine -- but what that is saying is that when you create an object, you also
introduce a variable. The opposite is not necessarily true: introducing a
variable does *NOT* imply the declaration of an object.

If that is the extent of what is discussed in the standard, then the short of
it is that the standard does not define what a variable is.
"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."


Seems like it isn't complete to me. Either the other ways (C-style?) to
instanciate an 'object' are either implied or just plain left out. Consider
the differences between:

int i; // created by a definition

int * j = new int; // by a new-expression

extern int k; // by the implementation

int * l = (int *)malloc(sizeof (int)); // what about me?
Nov 14 '05 #258
Julie wrote:
"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."

Fine -- but what that is saying is that when you create an object, you also
introduce a variable. The opposite is not necessarily true: introducing a
variable does *NOT* imply the declaration of an object.


No it says that you declare an object by using a variable. So when you
"declare" a variable in essence you declare an object.
If that is the extent of what is discussed in the standard, then the short of
it is that the standard does not define what a variable is.
It does. A variable (its name) denotes an 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."

Seems like it isn't complete to me. Either the other ways (C-style?) to
instanciate an 'object' are either implied or just plain left out. Consider
the differences between:

int i; // created by a definition

int * j = new int; // by a new-expression

extern int k; // by the implementation

Actually defined in another compilation unit.
int * l = (int *)malloc(sizeof (int)); // what about me?

In the above cases we have:

int i;

i is a variable that denotes an object.
int *j=new int;
j is a variable that denotes an int * object, that is the pointer itself.

The new int that is created in the free store is another object, not
denoted by a variable, since it is not created by a variable declaration.

extern int k; means that it is defined elsewhere (in general) and the
"int i" case applies for the definition.
The malloc() situation is the same with the new int situation.


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Nov 14 '05 #259
In comp.lang.c Ioannis Vranos <iv*@guesswh.at .grad.com> wrote:
Julie wrote:

extern int k; // by the implementation

Actually defined in another compilation unit.


Not necessarily. Consider the following, in the same translation unit.

static int a = 42;

extern int a; /* deceptive use of 'extern' */

--
Alex Monjushko (mo*******@hotm ail.com)
Nov 14 '05 #260

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
8493
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
2232
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
13124
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
2033
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
3588
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
20019
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
3133
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
9901
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
9752
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
11031
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...
0
10685
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10371
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...
0
9518
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7918
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
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4563
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.