473,854 Members | 1,457 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 20406
Irrwahn Grausewitz wrote:

Da*****@cern.ch (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?


I'm all for using correct terms. What would you call 'a' here:

int main()
{
int a = 1;
return 0;
}
Nov 14 '05 #201
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;
}

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 #202
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;
}

To help all people involved in this really boring discussion, here are
some definitions of the C++98 standard:
"A variable is introduced by the declaration of an object. The
variable’s name denotes the object."
"The constructs in a C++ program create, destroy, refer to, access, and
manipulate objects. 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.
The properties of an object are determined when the object is created.
An object can have a name (clause 3). An object has a storage duration
(3.7) which influences its lifetime (3.8). An object has a type (3.9).
The term object type refers to the type with which the object is created.

Some objects are polymorphic (10.3); the implementation generates
information associated with each such object that makes it possible to
determine that object’s type during program execution. For other
objects, the interpretation of the values found therein is determined by
the type of the expressions (clause 5) used to access them.

Objects can contain other objects, called sub-objects. A sub-object
can be a member sub-object (9.2), a base class sub-object
(clause 10), or an array element. An object that is not a sub-object
of any other object is called a complete object.

For every object x, there is some object called the complete object of
x, determined as follows:

— If x is a complete object, then x is the complete object of x.
— Otherwise, the complete object of x is the complete object of the
(unique) object that contains x.

If a complete object, a data member (9.2), or an array element is of
class type, its type is considered the most derived class, to
distinguish it from the class type of any base class sub-object; an
object of a most derived class type is called a most derived object.

Unless it is a bit-field (9.6), a most derived object shall have a
non-zero size and shall occupy one or more bytes of storage. Base class
sub-objects may have zero size. An object of POD4) type (3.9) shall
occupy contiguous bytes of storage.

[Note: C++ provides a variety of built-in types and several ways of
composing new types from existing types (3.9). ]"


Regards,

Ioannis Vranos
Nov 14 '05 #203
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".
Nov 14 '05 #204
Irrwahn Grausewitz <ir*******@free net.de> writes:
[...]
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".


If the C standard itself uses the word "variable" in its common,
somewhat informal sense, then I think we can get away with using it
here in the newsgroup(s).

In addition to a number of examples and footnotes, and ignoring
numerous uses as an adjective, the C99 standard uses the word
"variable" as a noun in normative text in 6.8.5.3p1, 7.6, and F.8.1p1.
(In my opinion, this usage is mildly sloppy, but not sloppy enough to
justify a DR.)

I think we can safely refer to a non-const-qualified object as a
"variable". Whether const-qualified objects are variables is a
question I won't try to answer.

--
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 #205
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cc******** **@sunnews.cern .ch...
In <nV************ *******@newssvr 29.news.prodigy .com> "Mabden" <mabden@sbc_glo bal.net> writes:
Let me help you out be repeating the question and answer for you, Dan.
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
generic answer based on the behaviour of your compiler. Such an answer
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?
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.
Furthermore, the specific behaviour of one compiler or another is usually
considered off topic in this newsgroup, you need a *good* reason for
invoking your compiler's behaviour.


"Brevity is the soul of wit". One can teach without a stick in hand, Dan.
YMMV! ;-) Lighten up, Dan.


Stop behaving like an idiot and I'll lighten up ;-)


Well then, there is no hope... :-P

--
Mabden
Nov 14 '05 #206
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?

Here's my proposal:

Technically correct:
'p' is an identifier designating an object of type pointer to int.
'*p' is an expression [dereferencing a pointer value] yielding an
lvalue of type int.

Jargon:
'p' is a pointer variable.
'*p' is a dereferenced address.

However, I'd never call '*p' a variable of type int, though others
mileage may (and obviously does) vary.

BTW: the term "pointer" on itself seems to be ambiguous; it's better
to distinguish "pointer object" or "pointer variable", and "pointer
value" or simply "address".

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 #207
[cross-post to clc++ deliberately stripped]

Keith Thompson <ks***@mib.or g> wrote:
Irrwahn Grausewitz <ir*******@free net.de> writes:
[...]
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".
If the C standard itself uses the word "variable" in its common,
somewhat informal sense, then I think we can get away with using it
here in the newsgroup(s).


Ok, but see below.
In addition to a number of examples and footnotes, and ignoring
numerous uses as an adjective, the C99 standard uses the word
"variable" as a noun in normative text in 6.8.5.3p1, 7.6, and F.8.1p1.
(In my opinion, this usage is mildly sloppy, but not sloppy enough to
justify a DR.)
However note that, interestingly, in the standard the term "variable"
is used solely to refer to named objects, but never to refer to
non-atomic expressions yielding modifiable lvalues.
I think we can safely refer to a non-const-qualified object as a
"variable".
I beg to disagree. By that definition *((char *)malloc(1)) and *"abc"
are variables.
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.

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 #208
Julie wrote:
Risto Lankinen wrote:
"Julie" <ju***@nospam.c om> wrote in message
news:40****** *********@nospa m.com...
Could you please post the relevant code that sets up two variables that


share
the same memory location?


This is a good start:

http://groups.google.fi/groups?selm=...y.DIALix.oz.au

Wha?! Forth? I don't see anything that even remotely describes how to set up
two variables that share the same memory location (_in_C_or_C++_, please).

When posting links, it is _most_ helpful if you first quote the relevant
portion(s), then post the referencing link...

Try again?


OK, I will. A C implementation of a doubly-linked list that uses the same
memory location for both forward and backward pointers follows at the end of
this post.

I suspect you won't be happy with it, but it does store two "conceptual
variables" in one memory location, at least. And it is a moderately
interesting technique, if not (I suspect) hugely useful.

Note that, by it's nature, it isn't portable, relying on being able to convert
a pointer to an unsigned int and back again.

I'm not really a C programmer, so apologies for any hideous blunders and
malapropisms. Additionally, I've left removing nodes as an exercise for the
reader (ahem).

Regards,
Kieran Elby
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

/*
Some platforms will need an unsigned long here.
Some platforms won't be able to use this technique at all.
*/
typedef unsigned int link;

typedef struct {
char *data;
/* This holds the XOR of the addresses of the
next and previous nodes */
link link;
} node;

/*
Add node after nodes n1 and n2.
n2 must be adjacent to n1.
*/
node* add_node(node *n1, node *n2) {

node *n, *n3, *n4;

n = malloc(sizeof(n ode));
n->data = 0;

if (!n2) {
n3 = 0;
} else {
n3 = (node *) (n2->link ^ (link) n1);
}

n->link = (link) n2 ^ (link) n3;

if (n2) {
n2->link = (link) n1 ^ (link) n;
}

if (n3) {
n4 = (node *) (n3->link ^ (link) n2);
n3->link = (link) n ^ (link) n4;
}

return n;
}

/*
Get node after nodes n1 and n2.
n2 must be adjacent to n1.
*/
node* next_node(node *n1, node *n2) {
if (n1) {
return (node *) (n2->link ^ (link) n1);
} else {
return (node *) n2->link;
}
}

void print_nodes(nod e *n1, node *n2) {

node *n;

while (1) {
printf(n2->data);
n = next_node(n1, n2);
n1 = n2;
n2 = n;
if (!n) {
break;
}
}
}

int main(void) {

node *nA, *nB, *nC, *nX;

/* Won't work without this */
assert(sizeof(l ink) >= sizeof(node *));

/* Create list of nodes A, B, C */

nA = add_node(0,0);
nA->data = "A";

nB = add_node(0,nA);
nB->data = "B";

nC = add_node(nA,nB) ;
nC->data = "C";

/* Add X between B and C */

nX = add_node(nA,nB) ;
nX->data = "X";

/* Traverse nodes forwards */

printf("Forward s: ");
print_nodes(0, nA);
printf("\n");

/* Traverse nodes backwards */

printf("Backwar ds: ");
print_nodes(0, nC);
printf("\n");

return 0;
}
Nov 14 '05 #209

"Irrwahn Grausewitz" <ir*******@free net.de> wrote in message
news:il******** *************** *********@4ax.c om...
[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
don't have write access too. But they're usually "volatile" because
someone else writes them.
Nov 14 '05 #210

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
13123
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
2032
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
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
10684
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
9517
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
7082
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
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
3
3188
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.