I order you to forgive yourself!
Hey all! In this thread, you can post your riddles so others can try and figure them out.
In order to start a new riddle, the previous riddles have to be solved or declared unsolvable or skipped (I guess this can be decided if no one likes the current riddle.).
Here is this first riddle. It's related to coding and programming languages.
//Assuming you have two variables, 'a' and 'b'. Find a way to swap them without the need for a third variable 'c'.
int a = 4;
int b = 5;
int c = a;
a = b;
b = c;
//Here a = 5 and b = 4.
Note: I'm pretty bad at starting those kind of games so any suggestions to improve the current setup is very appreciated.
Post has been edited 1 time(s), last time on Oct 17 2011, 7:39 pm by Apos. Reason: English mistakes...
//Assuming you have two variables, 'a' and 'b'. Find a way to swap them without the need for a third variable 'c'.
int a = 4;
int b = 5;
int d = a;
a = b;
b = d;
//Here a = 5 and b = 4.
Do I get a cookie?
None.
I order you to forgive yourself!
Loled, I should have said, without adding any other variables other than 'a' and 'b'.
int a = 4;
int b = 5;
b = a + b;
a = b - a;
b = b - a;
Followed in that order you should end up with a = 5, and b = 4.
I order you to forgive yourself!
int a = 4;
int b = 5;
b = a + b;
a = b - a;
b = b - a;
First riddle solved!
Why would you ever code that way...
None.
Why would you ever code that way...
For the purpose of a riddle.
Why would you ever code that way...
I did things like that a lot when trying to cobble together arithmetic in starcraft triggers.
None.
Why would you ever code that way...
In early/low-powered computers, they didn't have a lot of resources to spare for extra variables and such. It's doubtful that multiple arithmetic operations would ever be considered a good trade off, unless you absolutely had to do it, though.
None.
Addition/subtraction aren't terribly expensive operations, though.
None.