-
March 4th, 2005, 07:08 PM
#1
Member
A brain teasing exercise??
HI Pals
ok, you all have asked some of triky question(s)... try to make it......
all you have to do is write a c/c++ code for adding two numbers
BUT
1.you can not use any of the operator (directly or indirectly ie. sum must not be calculated by using any opperator)nor even bitwise.
eg.
making a link list equivalent to sum of two numbers and
showing count of the list member as sum is invalid because
u finaly uses '+' operator to calculate sum.
2. you can not use any pre-define function of any header file.
try to make it done without playing with registers
just a simple code........... if you can...... post it here.
please do not post any thing which is not related to this post.
queries regarding the question are most welcome.
it's a braneware
smoking is really bad for ur PC....
-
March 5th, 2005, 12:08 AM
#2
Hi
Reminds me of another brainteaser, which actually has a solution:
Create a combination of templates in c++, which creates errors
while compiling/linking, occuring at (precompiled) line-numbers, which
correspond to prime numbers ... figure THAT out
Cheers
P.s. should be in tech humor, shouldn't it ...
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
-
March 5th, 2005, 02:52 AM
#3
hmmm dont know c++ but I think i know a theory which could solve it.
by breaking the nuber apart and storing it as a string and then using a series of if/else statements to produce an output.
For example if the first number was 123 and the second was 456
it would take each number of first and store them as seperate array entrys like
myArray[0] = "1"
myArray[1] = "2"
myArray[2] = "3"
and the same for the second number and then compare the entrys using either if statements or switch (this is from actionscripting switch/case/break - but am sure c++ has something similar) could then be used to produce an end figure like
[this is an example in actionscript]
Code:
if ((myArray[0] == "1") and (myArray2[0] == "1")) {
myResult[0] = "2";
} else if ((myArray[0] == "1") and (myArray2[0] == "2")) {
myResult[0] = "3";
} else if ((myArray[0] == "1") and (myArray2[0] == "3")) {
myResult[0] = "4";
}
and so on up to 9 and then do it in reverse order increasing myArray by 1 each time rather than myArray2
do this for each entry in the arrays and then implode the myResult array to get the actual result. I coul probably do this in actionscript/PHP if I had time but as I said am afraid I dont know c++
but the basic theory I think is sound and should work......
so do i get a cookie?
v_Ln
-
March 5th, 2005, 03:08 AM
#4
Try this ........
Find if a given number is even or odd in C using just the basic input/output features.
NO "if", NO "switch" and all those other constructs .....
Its simple
-
March 5th, 2005, 04:01 AM
#5
Member
hi
VAl~ you got it right, but are there any other methord also.
sec_ware i dont have knowledge of templates but will deffinatly try to that when....
Packet~ only 'if' and 'switch' are not allowed, rest we can use any thing.....
lokeshdb_ms
smoking is really bad for ur PC....
-
March 6th, 2005, 08:32 AM
#6
hmm ......
Guess i'll give the logic ...
scanf(&n); // Read the Number
int a[ ]={"Even","Odd"}; // Initialize an array
printf("The number is %d",a[n%2]); // Easy as a pie
-
March 6th, 2005, 06:05 PM
#7
Member
thanx packet~,
but it can also be done by using
a loop n a "n%2?condition:condition"
ok another question
swap 2 numbers without using any third varriable or pointer.???
it's realy geting hot in here..
smoking is really bad for ur PC....
-
March 7th, 2005, 05:03 AM
#8
PacketThirst -- That was my solution here http://www.antionline.com/showthread...hreadid=262389
Val's can be done by precomputing a 2 dimensional array. IE, if the numbers to add are 2 + 5, then answers[2][5] would return 7, etc.
For swaping 2 numbers, here is my solution:
inputs: num1 = 50, num2 = 42.
operation: num1 = num1 - num2 = 50 - 42 = 8
display: num1 = 8; num2 = 42
operation : num2 = num2 + num1 = 42 + 8 = 50
display: num1 = 8; num2 = 50
operation: num1 = num2 - num1 = 50 - 8 = 42
display: num1 = 42; num2 = 50
DONE
-
March 7th, 2005, 02:54 PM
#9
1.you can not use any of the operator (directly or indirectly ie. sum must not be calculated by using any opperator)nor even bitwise.
but it can also be done by using
a loop n a "n%2?condition:condition"
Code:
% <-- the modulus binary operator
? <-- an unary operator
You said no operators.
ac
-
March 11th, 2005, 09:27 AM
#10
Member
hi
gothic_type u missunderstood my question.
1. "u can not use anay operator" condition is for adding two numbers.
2. one is the reply for the post of packet~.
thanx tim~ it's a good logic.
here's another one ...... question say you can not use third varriable but u can use third memory location ............................ got it .............................. you can use a array insted
smoking is really bad for ur PC....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|