Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - Puzzles

 
Thread Tools Search this Thread
Old 08-07-2003, 07:48 PM   #21
Default Re: Puzzles


Girish Pal Singh wrote:
> I will be greatful if any one answers these questions?
> Thank You.
> 1. Write a "Hello World" program in 'C' without using a semicolon.


Ok. 'a "Hello World" program in 'C' without using a semicolon'

> 2. Write a C++ program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;


int main()
{
cout << "numbers from 1 to 100 and 100 to 1;" << endl;
}

Why are you asking C++ questions in a C newsgroup?

>
> 3. Find if the given number is a power of 2.


What's the given number?

> 4. Multiply x by 7 without using multiplication (*) operator.


y = pow(10.0, log10(x)+log10(7));

> 5. Write a function in different ways that will return f(7) = 4 and
> f(4) = 7


char* a_function_in_different_ways()
{
return "f(7) = 4 and f(4) = 7";
}

> 6. Remove duplicates in array


for (loop = 0; loop <= elements; loop++)
array[loop] = 0;
elements = 0;

It also has the added feature of removing the non-duplicates too.

> 7. Finding if there is any loop inside linked list.


trace = first
while(trace)
{
if (trace.value = loop)
printf("Here it is!");
trace=trace.next;
}

> 8. Remove duplicates in an no key access database without using an
> array


Just delete everything.

It also has the added benefit of removing non-duplicates too.

> 9. Convert (integer) number in binary without loops.


convert("(integer) number in binary without loop");

Are we supposed to rot13 it, or what?

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


int main()
{
printf("an exact copy of the source.\n");
}

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


So?

No computer in this question. Only several people playing
in the water.

> 13. Swap two numbers without using a third variable.


temp = a;
a = b;
b = temp;

See, i never used "a third variable" in that code sequence.
I guess you could

temp2 = "a third variable"
temp = a;
a = b;
b = temp;

but it just adds bloat. Or is it supposed to be in a comment?


Do I win any prizes?




Kevin Handy
  Reply With Quote
Old 08-08-2003, 02:58 AM   #22
Jens Schicke
 
Posts: n/a
Default Re: Puzzles
Girish Pal Singh <> wrote:
> I will be greatful if any one answers these questions?
> Thank You.
> 1. Write a "Hello World" program in 'C' without using a semicolon.


#define HEL "4$"
#define LO "c%"
#define WOR "s%2"
#define LD "$"
#define _ LD LO
#define HELLO HEL LO
#define WORLD "1"

void main(void)
{
if(printf("%"LO HELLO WORLD HEL WOR WORLD _"2"_ WORLD"3"LD"s%5"LD"s%" WORLD HEL WOR HELLO"28"LD"c",
'H', 'o', 'm', 'e', "Wor", 'k', ' ',
's', 'u', 'c', 'k', 's', " ", "l", 'i', "k", 'e',' ',
'h', 'e', 'l', 'l', ' ', 'd', "u", "d", 'e', '\n'))
{
}
}

> 2. Write a C++ program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;


int dargc = 1;

int main(int argc, char* argv[])
{
return argc ?
printf("%d\n", argc),
((argc == 100) ?
printf("%d\n", (dargc -= 2, argc)) : 0x41627763),
main(argc+dargc, (char **) 0x77624163) : 0x41637762;
}

> 3. Find if the given number is a power of 2.


long testpow2(long n)
{
n = (n & 0xFFFF) + ((n & 0xFFFF0000) >> 16);
n = (n & 0xFF) + ((n & 0xFF00) >> ;
n = (n & 0xF) + ((n & 0xF0) >> 4);
n = (n & 0x3) + ((n & 0xC) >> 2);
n = (n & 0x1) + ((n & 0x2) >> 1);
return !--n;
}

> 4. Multiply x by 7 without using multiplication (*) operator.


#define mult7(x) (((x) << 4) - ((x) << 3) - ((x) << 2) + ((x) << 1) + (x))


Jens Schicke
  Reply With Quote
Old 08-08-2003, 07:49 AM   #23
Martin Ambuhl
 
Posts: n/a
Default Re: Puzzles
Jens Schicke wrote:

> Girish Pal Singh <> wrote:
>
>>I will be greatful if any one answers these questions?
>>Thank You.
>>1. Write a "Hello World" program in 'C' without using a semicolon.

>
>
> #define HEL "4$"
> #define LO "c%"
> #define WOR "s%2"
> #define LD "$"
> #define _ LD LO
> #define HELLO HEL LO
> #define WORLD "1"
>
> void main(void)


The specification asked for a program in C. You've just fallen off the wagon.



--
Martin Ambuhl



Martin Ambuhl
  Reply With Quote
Old 08-08-2003, 05:47 PM   #24
Don Starr
 
Posts: n/a
Default Re: Puzzles
On Fri, 08 Aug 2003 07:57:42 -0700, Kelsey Bjarnason <> wrote:

>[snips]
>
>>> 3. Find if the given number is a power of 2.

>>
>> long testpow2(long n)
>> {
>> n = (n & 0xFFFF) + ((n & 0xFFFF0000) >> 16);
>> n = (n & 0xFF) + ((n & 0xFF00) >> ;
>> n = (n & 0xF) + ((n & 0xF0) >> 4);
>> n = (n & 0x3) + ((n & 0xC) >> 2);
>> n = (n & 0x1) + ((n & 0x2) >> 1);
>> return !--n;
>> }

>
>Does this work if a long is 64 bits and the bit set is one of the upper 32?
>


How about this:
int testpow2( long n )
{
return n ? 1 : 0;
}

I didn't see any restrictions in the original post (based on the quoted text above) stating that the
test was whether or not the "given number" was an integral, rational, or even real power of 2. (I
also didn't see any requirement that the test number be an integer - just using it here as an
example).




Don Starr
  Reply With Quote
Old 08-12-2003, 06:51 AM   #25
R. Rajesh Jeba Anbiah
 
Posts: n/a
Default Re: Puzzles
(Girish Pal Singh) wrote in message news:<. com>...
> I will be greatful if any one answers these questions?
> Thank You.


These questions are frequently asked interview questions in
India. You would get flames if you post these "fun" questions (except
to Indians). You can get most of the answers by Googling
(www.google.com). Few answers are availabled at "A to Z of C" project
page ( http://guideme.itgo.com/atozofc/ )...

> 1. Write a "Hello World" program in 'C' without using a semicolon.


/* author: Joona I Palaste ??? */
#include <stdio.h>
int main ()
{
if ( printf ( "Hello World\n" ) > 0 )
{
}
}


> 2. Write a C++ program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;


//author: richard heathfield ???

#include <stdio.h>

#define TOP 100

int foo(int i)
{
printf("%d\n", i);
i < TOP && foo(i + 1);
printf("%d\n", i);
return i;
}

int main(void)
{
foo(1);
return 0;
}

> 3. Find if the given number is a power of 2.


Answer available in "A to Z of C"

> 4. Multiply x by 7 without using multiplication (*) operator.


//Logic: (n<<3)-n
//author: R. Rajesh Jeba Anbiah
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
for(i=0; i<10; ++i)
printf("%d*7 = %d\n", i, (i<<3)-i);
return 0;
}

> 5. Write a function in different ways that will return f(7) = 4 and
> f(4) = 7
> 6. Remove duplicates in array


//author: ??
int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++;
insert++;
}
else
current++;
return insert;
}

> 7. Finding if there is any loop inside linked list.
> 8. Remove duplicates in an no key access database without using an
> array
> 9. Convert (integer) number in binary without loops.


Answer available in "A to Z of C"

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


Answer available in "A to Z of C"

> 11. 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.
> 13. Swap two numbers without using a third variable.


Answer available in "A to Z of C"

HTH,
R. Rajesh Jeba Anbiah

---
"We live to die; We die to live"
Email: rrjanbiah-at-Y!com


R. Rajesh Jeba Anbiah
  Reply With Quote
Old 08-13-2003, 02:24 AM   #26
ArWeGod
 
Posts: n/a
Default Re: Puzzles
IF is not a looping construct. You can use recursion, so long as IF is
available.



"Kelsey Bjarnason" <> wrote in message
news htspeed.bc.ca...
> [snips]
>
> On Fri, 08 Aug 2003 03:58:50 +0200, Jens Schicke wrote:
>
> >> 2. Write a C++ program without using any loop (if, for, while etc) to
> >> print numbers from 1 to 100 and 100 to 1;





ArWeGod
  Reply With Quote
Old 06-21-2008, 01:35 PM   #27
lubna hussaini
Junior Member
 
Join Date: Jun 2008
Posts: 1
Default
Quote:
Originally Posted by Malcolm
"Girish Pal Singh" <> wrote in message
> I will be greatful if any one answers these questions?
>

I wonder what you tutor is looking for here. Maybe these are end of term
"fun" questions;
>
> 1. Write a "Hello World" program in 'C' without using a semicolon.
>

Hint - use an "if" statement.
>
> 2. Write a C++ program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;
>

Brute force this is easy. Try clc++ for further hints.
>
> 3. Find if the given number is a power of 2.
>

You need to check that only one bit is set.
>
> 4. Multiply x by 7 without using multiplication (*) operator.
>

This is called backcracking
x * constant
can always be replaced by
(x << a) + (x << b) ...
>
> 5. Write a function in different ways that will return f(7) = 4 and
> f(4) = 7
>

presumably without using an if statement. You need to look at bitwise
operators.
>
> 6. Remove duplicates in array
>

This is actually a real task. It can be done by using nested loops.
>
> 7. Finding if there is any loop inside linked list.
>

You need to store the address of one of the nodes, then check if you revisit
it.
>
> 8. Remove duplicates in an no key access database without using an
> array
>

Got me here. "Never bullshit your way into a databse job".
>
> 9. Convert (integer) number in binary without loops.
>

Unroll the loop
>
> 10. 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.
>

look up "quine" on the web.
>
> 11. 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.
>

Your question?
>
> 13. Swap two numbers without using a third variable.
>

This is a stupid hack. I'll give you this one.

a ^= b; a ^= b; a ^= b.
another solution to this question: a=a+b; b=a-b;a=a-b;


lubna hussaini
lubna hussaini is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46