473,838 Members | 1,694 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 20374
>> In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes: [snippage -- but "a" and "b" are distinct objects of type "int"]
printf("address es are %s\n", &a == &b ? "equal" : "unequal");
... If [this] prints
a = 10, b = 20, addresses are equal
does it imply that the compiler is non-conforming?
Chris Torek <no****@torek.n et> writes:
In C at least, yes it does. (I would be quite surprised if the
answer changes for C++.)

In article <news:ln******* *****@nuthaus.m ib.org>
Keith Thompson <ks***@mib.or g> writes:I believe you, but how does the standard imply this?


The most important item is the description of the "!=" operator.
This is from a C99 draft, but the key wording is unchanged from
C89:

6.3.9 Equality operators
...
Semantics
...
[#4] If two pointers to object or incomplete types are both
null pointers, they compare equal. If two pointers to
object or incomplete types compare equal, they both are null
pointers, or both point to the same object, or both point
one past the last element of the same array object.79 ...

One also must look at the unary-& operator, of course; but if &a == &b,
the two pointers involved are:

- both NULL (better not be), or

- both point to the same object (better not; "a" and "b" are not
the same object, even if they occupy the same physical location
*after* optimization), or

- both point one past the last element of the same array object.

Since none of these is true, the result of the "==" and "!="
operators must be "not equal".

I think the stickiest point here really lies in figuring out what
defines a "unique" or "distinct" object. Other than via unions,
or nonportable assembly tricks like __asm__(".globa l a, b; b = a"),
objects with different "ordinary variable names" must be distinct,
although the inverse is not true (same-name can still be distinct):

int a, b, c; /* a, b, and c are all distinct */
void f(void) {
int a;
extern int b;
static char *c;
/* f()'s a and c are not the same as the file-scope a and c,
while f()'s b is the same as the file-scope b. */
...
}

Unions make a mockery of distinctness:

union {
char a;
float b;
long double complex c; /* c99 only */
} u;

Here u.a, u.b, and u.c are not actually distinct objects, although
sizeof(u.a) is 1, and probably sizeof(u.b) is greater than 1
(typically 4) while sizeof(u.c) is probably greater still (typically
16, 24, or 32 today), so that "most of" u.c is probably distinct
from u.a, in a way.

One can also make a mess with macros, but in general, if you see
C code that refers to "i" and then "j", these will be distinct
objects. Even more complicated names/expressions like s.x and s.y
or p->x and p->y *probably* refer to distinct objects.

The rules for whether identical ordinary identifiers refer to the
same underlying object *are* spelled out (in great detail) in the
Standard, but this requires going through the sections on scope
and linkage. Note that there are cases where "logically distinct"
objects *are* allowed to share underlying memory, but the only
way to test it is to invoke undefined behavior:

void f(void) {
int *p;
{
int i;
p = &i;
}
/* BEWARE: the value in p is now "indetermin ate" */
{
int j;
printf("i (which no longer exists) and j do%s occupy "
"the same space\n", (p == &j) ? "" : " not");
/* ERROR -- use of the indeterminate value in p produces
undefined behavior */
}
}

This function will generally compile and run (perhaps with a warning
from a particularly clever compiler) but produces undefined behavior,
so one cannot prove anything from it. Your (Keith's) original
example *does* have defined behavior, and must claim that the
addresses are unequal.
--
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 #271
Chris Torek <no****@torek.n et> writes:
In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes: [snippage -- but "a" and "b" are distinct objects of type "int"] printf("address es are %s\n", &a == &b ? "equal" : "unequal");
... If [this] prints
a = 10, b = 20, addresses are equal
does it imply that the compiler is non-conforming?

Chris Torek <no****@torek.n et> writes:
In C at least, yes it does. (I would be quite surprised if the
answer changes for C++.)


In article <news:ln******* *****@nuthaus.m ib.org>
Keith Thompson <ks***@mib.or g> writes:
I believe you, but how does the standard imply this?


The most important item is the description of the "!=" operator.
This is from a C99 draft, but the key wording is unchanged from
C89:

6.3.9 Equality operators
...
Semantics
...
[#4] If two pointers to object or incomplete types are both
null pointers, they compare equal. If two pointers to
object or incomplete types compare equal, they both are null
pointers, or both point to the same object, or both point
one past the last element of the same array object.79 ...

[snip]

Yes, that does it. Thanks.

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

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
9853
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
10584
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...
1
10644
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
9423
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
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
7014
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...
3
3131
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.