473,854 Members | 1,512 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 20404
Da*****@cern.ch (Dan Pop) wrote in message news:<cb******* ***@sunnews.cer n.ch>...

<snip>
My point was that it is sheer nonsense to say "you'll get a warning about
that" if the standard doesn't *require* a diagnostic.
It is one thing to point out what the standard says, what messages
should be there, etc. It is another thing to get into a harsh argument
about it with someone else.

I entirely agree that the code deserves a diagnostic, even when compiled
in C99 mode, but this is something completely different.

Dan

Nov 14 '05 #191
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cb******** **@sunnews.cern .ch...
In <a7************ **************@ posting.google. com> fo******@yourem ailbox.com (Foobarius Frobinium) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<cb******* ****@sunnews.ce rn.ch>...
In <zo************ ****@newssvr27. news.prodigy.co m> "Mabden" <mabden@sbc_glo bal.net> writes:
>"Foobarius Frobinium" <fo******@youre mailbox.com> wrote in message
>news:a7******* *************** ****@posting.go ogle.com...
>> js*******@sanch arnet.in (Jatinder) wrote in message
news:<22******* *************** ****@posting.go ogle.com>...
>> You mentioned MS often use these sorts of puzzles to test their
>> programmers. I got an even better one to stump even the best MS
>> programmers:
>> #include <stdio.h>
>>
>> int main(void) {
>> int *foo = NULL;
>>
>> fprintf(stdout, "%i\n", *foo);
>> }
>>
>> What will happen if I compile and run this program??
>
>You'll get a warning saying main() has no return value.

Chapter and verse, please.


Well, you should, depending on compiler flags.


Sez who?
gcc -Wall gives you:

foo.c: In function `main':
foo.c:10: warning: control reaches end of non-void function


Add a -std=c99 to your gcc invocation and the warning goes away.

My point was that it is sheer nonsense to say "you'll get a warning about
that" if the standard doesn't *require* a diagnostic.

I entirely agree that the code deserves a diagnostic, even when compiled
in C99 mode, but this is something completely different.


Actually, it is the OP's question that I answered.

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.

Theory was not requested, not a specific implementation. On my box, that is
what will happen.

YMMV! ;-) Lighten up, Dan.

--
Mabden
Nov 14 '05 #192
In <nV************ *******@newssvr 29.news.prodigy .com> "Mabden" <mabden@sbc_glo bal.net> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cb******* ***@sunnews.cer n.ch...
In <a7************ **************@ posting.google. com>fo******@youre mailbox.com (Foobarius Frobinium) writes:
>Da*****@cern.c h (Dan Pop) wrote in messagenews:<cb****** *****@sunnews.c ern.ch>... >> In <zo************ ****@newssvr27. news.prodigy.co m> "Mabden"<mabden@sbc_gl obal.net> writes: >>
>> >"Foobarius Frobinium" <fo******@youre mailbox.com> wrote in message
>> >news:a7******* *************** ****@posting.go ogle.com...
>> >> js*******@sanch arnet.in (Jatinder) wrote in message
>> news:<22******* *************** ****@posting.go ogle.com>...
>> >> You mentioned MS often use these sorts of puzzles to test their
>> >> programmers. I got an even better one to stump even the best MS
>> >> programmers:
>> >> #include <stdio.h>
>> >>
>> >> int main(void) {
>> >> int *foo = NULL;
>> >>
>> >> fprintf(stdout, "%i\n", *foo);
>> >> }
>> >>
>> >> What will happen if I compile and run this program??
>> >
>> >You'll get a warning saying main() has no return value.
>>
>> Chapter and verse, please.
>
>Well, you should, depending on compiler flags.


Sez who?
>gcc -Wall gives you:
>
>foo.c: In function `main':
>foo.c:10: warning: control reaches end of non-void function


Add a -std=c99 to your gcc invocation and the warning goes away.

My point was that it is sheer nonsense to say "you'll get a warning about
that" if the standard doesn't *require* a diagnostic.

I entirely agree that the code deserves a diagnostic, even when compiled
in C99 mode, but this is something completely different.


Actually, it is the OP's question that I answered.

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.

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.
Theory was not requested, not a specific implementation. On my box, that is
what will happen. ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
Did the OP ask what will happen on *your* box? If not, what *exactly* is
your point, especially since you're admitting yourself that the question
was not about a specific implementation?
YMMV! ;-) Lighten up, Dan.


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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #193
In <c3************ **************@ posting.google. com> sc************@ comcast.net (Scott Jacobsen) writes:
Ioannis Vranos <iv*@guesswh.at .grad.com> wrote in message news:<cb******* ****@ulysses.no c.ntua.gr>...
int main (void)
{
if (printf("Hello World")) {}

/* Only needed for C90 compliance */
if (exit(EXIT_SUCC ESS),1){}
}
Here was my less elegant solution. But I didn't even use anything that
looks like a ';'. (commas look a lot like semicolons...) :)

#include <stdio.h>
#include <stdlib.h>

int main() {
while(printf("h ello\n") && 0) {
}
while(((int (*)(int))(exit) )(0) || 1) {}

^^^^^^^^^^^^^^^ ^^^^^^^^^^
Undefined behaviour. You can convert the address of exit to an
incompatiple function pointer type, but you cannot dereference the
result without invoking undefined behaviour. Your code is akin to
calling exit() without including <stdlib.h> in C89: you can't call
a void function as if it were an int function.

It is not possible to call a void function in such a program without
using the comma operator. Fortunately, no C or C++ standard requires
the function main to terminate through an exit call or a return statement.
}


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

That said, I would not place the equality/inequality check for the
possibility of the same variable passed in mind, but for efficiency.


If you have efficiency in mind, you better leave out the comparison
until tests show that the 'problem' it solves really happens that
often, that it makes a difference.

In any ordinary program one would not expect that special case to
happen very often. I would estimate around 1 out of 100 where this
'optimization' shortcut really is taken. That means: you slow down
99 cases just to speed up 1 case.

Same with the comparison for equality in an operator= (Sorry for
being off topic in comp.lang.c). If it is written exception safe,
there is no need for the test with 'this'. Especially not for
optimizaztion, since the case you are optimizing doesn't happen
that often in any typical program. But all other cases suffer
from a needless comparison.

--
Karl Heinz Buchegger
kb******@gascad .at
Nov 14 '05 #195
In <jk************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
<my two cents>

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" .
5. I suggest to drop the term "variable" all together, *especially* in
cross-posts between c.l.c and c.l.c++, since it's not defined at
all in the C standard and IMHO somewhat vaguely defined in the C++
standard.


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.

By the time c.l.c is officially decreed a select club of initiated
regulars, there would be no need to ban these terms, because no one would
use them, anyway.

I guess similar considerations apply to c.l.c++...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #196
Dan Pop wrote:
In <c3************ **************@ posting.google. com>
sc************@ comcast.net (Scott Jacobsen) writes:
Here was my less elegant solution. But I didn't even use anything that
looks like a ';'. (commas look a lot like semicolons...) :)

#include <stdio.h>
#include <stdlib.h>

int main() {
while(printf("h ello\n") && 0) {
}
while(((int (*)(int))(exit) )(0) || 1) {}

^^^^^^^^^^^^^^^ ^^^^^^^^^^
Undefined behaviour. You can convert the address of exit to an
incompatiple function pointer type, but you cannot dereference the
result without invoking undefined behaviour. Your code is akin to
calling exit() without including <stdlib.h> in C89: you can't call
a void function as if it were an int function.

It is not possible to call a void function in such a program without
using the comma operator. Fortunately, no C or C++ standard requires
the function main to terminate through an exit call or a return statement.


Yeah. I know. But it works on my system :) And exit never returns, so there
is no reason to worry about the return value (or lack there of). All you
have to worry about is that the next instruction is that start of the exit
function. After that nothing matters.

--
Scott
Nov 14 '05 #197
On Mon, 28 Jun 2004 04:16:52 +0300, Ioannis Vranos wrote:
Given an array (group) of numbers write all the possible sub groups of
this group.

std::next_perm utation and std::prev_permu tation. An example:
#include <algorithm>
#include <string>
#include <iostream>
int main()
{
using namespace std;

string s="abcd";
do
cout<<s<<endl;
while(next_perm utation(s.begin (), s.end()));
}


and if
unsigned s={12222, 839939, 9393999, 9099999, 98394763, 89383883};
Next_permutatio n is ok?
if not for me next_permutatio n==fuochino, deve essere modificata
esiste una soluzione non ricorsiva
Nov 14 '05 #198
On Thu, 01 Jul 2004 16:14:31 GMT, RoSsIaCrIiLoIA <n@esiste.ee> wrote:
On Mon, 28 Jun 2004 04:16:52 +0300, Ioannis Vranos wrote:
> Given an array (group) of numbers write all the possible sub groups of
> this group.std::next_per mutation and std::prev_permu tation. An example:
#include <algorithm>
#include <string>
#include <iostream>
int main()
{
using namespace std;

string s="abcd";
do
cout<<s<<endl;
while(next_perm utation(s.begin (), s.end()));
}


and if
unsigned s={12222, 839939, 9393999, 9099999, 98394763, 89383883};
Next_permutati on is ok?

unsigned *s = {12222, 839939, 9393999, 9099999, 98394763, 89383883};
if not for me next_permutatio n==fuochino, deve essere modificata
esiste una soluzione non ricorsiva


it is wrong excuse me
Nov 14 '05 #199
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?

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

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
9751
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
9513
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
7917
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
5743
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
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
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.