|
-
Aug 25th, 2000, 03:17 PM
#1
Thread Starter
Fanatic Member
Most highly trained and skilled professors, programmers, text books, etc. claim that to swap two variables, A and B, so that A contains B's value and B contains A's value, you need a third temporary variable, C.
Code:
Dim a As Integer
Dim b As Integer
Dim c As Integer
Let a = 10
Let b = 20
'
' Swap them...
'
Let c = a ' c is 10
Let a = b ' a is 20
Let b = c ' b is 10
Now, if you guys are so smart, then let me say this: There is a way to swap the two variables WITHOUT a third variable. Can anyone figure out this little riddle?
If you want a hint, wait for a few days, and I will post it.
Let's see how clever you all are.
-
Aug 25th, 2000, 03:27 PM
#2
Hyperactive Member
Probably not a very elegant solution, but here goes.
dim a as string
dim b as string
a="Hello"
b="There"
open "c:\test.txt" for random as #1
put #1, 1, a
close #1
a = b
open "c:\test.txt" for random as #1
get #1, 1, b
close #1
et voila!!!
-
Aug 25th, 2000, 04:04 PM
#3
Frenzied Member
I GOT IT AFTER LIKE 1 HOUR:
Code:
'I give credit to oetje for helping me with the positive and negative stuff
Private Sub Form_Load()
Dim a As Integer
Dim b As Integer
Let a = -753
Let b = 233
'Let b = a ' 10 is 10
'Let a = b ' b is 20
If b > a Then
b = b - a
a = a + b
b = b - a
'****help from oetje
b = b - (2 * b)
'*******************
Else
b = b - a
a = a + b
b = b - a
'****help from oetje
b = Abs(b)
'*******************
End If
Label1.Caption = a
Label2.Caption = b
End Sub
add 2 labels and run it to see for your self
NXSupport - Your one-stop source for computer help
-
Aug 25th, 2000, 04:16 PM
#4
Frenzied Member
now do I get a nobel prize award for it? or a million dollars? or even better... A Lolli POP
NXSupport - Your one-stop source for computer help
-
Aug 25th, 2000, 04:29 PM
#5
Frenzied Member
Use Xor
For some pairs of variables, Xor can be used to swap values.
Code:
A = A Xor B
B = A Xor B '(A Xor B) Xor B = A
A = B Xor A 'A Xor (A Xor B) = B
I would not be sure that the above would work if A, B are different length or different types of variables.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Aug 25th, 2000, 05:01 PM
#6
_______
<?>
Code:
'Label1,Label2,Text1,Text2,Command!,Form1
'the only sticky part was using double to avoid overflow
'of course if you are moving words that is another story
'for another time
Private Sub Command1_Click()
Dim a As Double
Dim b As Double
a = Text1.Text
b = Text2.Text
a = CDbl(a)
b = CDbl(b)
Label1.Caption = "a = " & a & " " & "b = " & b
b = b * a
a = b / a
b = b / a
Label2.Caption = "a = " & a & " " & "b = " & b
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 25th, 2000, 05:04 PM
#7
Frenzied Member
thats aalmost the same thing that I did except I didn't add textboxes
NXSupport - Your one-stop source for computer help
-
Aug 25th, 2000, 05:08 PM
#8
Thread Starter
Fanatic Member
GUV is smart
Yeah man.
GUV, you've got it right. The correct answer is use XOR.
Code:
Dim a As Integer
Dim b As Integer
Let a = 'anything
Let b = 'anything
Let a = a Xor b
Let b = a Xor b
Let a = a Xor b
I really like dimawa's solution and also the one that came from CyberSurfer.
However, although this problem has been solved: What would you do if you could NOT use Xor and no external storages, including but not limited to (heard that somewhere), clipboard, files, variables, web servers, haha.
You can still use logical operators such as AND, OR, but you CAN'T use XOR.
-
Aug 25th, 2000, 05:10 PM
#9
Frenzied Member
I didn't use any other stuff like that, no Xor and no external storages, including but not limited to (heard that somewhere), clipboard, files, variables, or web servers
NXSupport - Your one-stop source for computer help
-
Aug 25th, 2000, 05:18 PM
#10
Thread Starter
Fanatic Member
Dear dima
I am not gonna judge you...
I haven't tried out your solution, but does it work with ANY value for a and b???
I know this was not explicitly an initial requirement, but is now, haha
Please don't hate me too much
-
Aug 25th, 2000, 05:23 PM
#11
Frenzied Member
1. I didn't get the inslut
2. yes it does woerk with any numerical value
NXSupport - Your one-stop source for computer help
-
Aug 25th, 2000, 05:41 PM
#12
Thread Starter
Fanatic Member
dimawa
OK, I admit I have still not tried it out, mainly because of time pressure, but I will.
If it turns out your solution works, then poor Guv will have to share the victory with you. (The first one), the second victory would go to you. But, I gotta check it out first. So, tomorrow, is the big day.
All you guys, keep those suggestions coming in. Surely there must be more than one simple solution to such a simple problem.
-
Aug 25th, 2000, 07:55 PM
#13
Frenzied Member
A+B?
It occurred to me that for numeric variables, the following would work if roundoff/truncation does not throw a monkey wrench into the works.
Code:
A = A + B
B = A - B '(A+B)-B = A
A = A - B '(A+B)-A = B
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Aug 25th, 2000, 07:57 PM
#14
Frenzied Member
NXSupport - Your one-stop source for computer help
-
Aug 25th, 2000, 08:05 PM
#15
Frenzied Member
Construct Xor?
If construction of Xor from And, Or, & Not is allowed, you can do just that.
(A Or B) And Not(A And B) is equivalent to Xor.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Aug 25th, 2000, 08:28 PM
#16
Frenzied Member
well what you are saying is that what I made is the same thing as xor?
NXSupport - Your one-stop source for computer help
-
Aug 26th, 2000, 04:36 AM
#17
another riddle
what is the fastest way to reverse a 30,000 character string?
this should keep you occupied.
hint, it can be done in 0.07 of a second on a P200MMX PC.
Have fun
-
Aug 26th, 2000, 12:04 PM
#18
Frenzied Member
wait wait wait, let hax soft say that I'm right first
NXSupport - Your one-stop source for computer help
-
Aug 26th, 2000, 12:22 PM
#19
Thread Starter
Fanatic Member
Hi dima
dimava, You're right.
You are just fabulous and smart, haha
-
Aug 26th, 2000, 12:42 PM
#20
Frenzied Member
thanks, I KNEW I WAS CORRECT
NXSupport - Your one-stop source for computer help
-
Aug 26th, 2000, 01:43 PM
#21
Thread Starter
Fanatic Member
That string reversing question is very interesting ... are we working with or without Windows API calls?
-
Aug 26th, 2000, 03:44 PM
#22
Frenzied Member
Xor leaps in battle again.
I think the following might work (I did not test it).
Code:
Private Sub Reverser(LongStr As String)
Dim N As Integer
Dim M As Integer
Dim A As Integer
Dim Z As Integer
N = Len(LongStr)
M = N / 2
Z = N
For A = 1 to M
Mid(LongStr, A, 1) = Mid(LongStr, A, 1) _
Mid(LongStr, Z, 1)
Mid(LongStr, Z, 1) = Mid(LongStr, A, 1) _
Mid(LongStr, Z, 1)
Mid(LongStr, A, 1) = Mid(LongStr, A, 1) _
Mid(LongStr, Z, 1)
Z = Z - 1
Next A
End Sub
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Aug 26th, 2000, 04:17 PM
#23
Member
I think the easyest way would be:
StringB = StrReverse(StringA)
-
Aug 26th, 2000, 04:55 PM
#24
Another brainteaser
How do you store the structure of a whole hard drive in a treeview without using a recursive loop?
No controls, type libraries, dos commands are allowed.
BTW and FYI, I don't have the answer
-
Aug 27th, 2000, 05:28 AM
#25
I dont think its possible without using a recursive loop, unless the FileSystemObject has a method to do it. Why would you constrain yourself to not using recursion? that would only become a problem if you had thousands of nested directories, and then call stack space would fill up fast.
[Edited by wossname on 08-27-2000 at 06:31 AM]
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
|
Click Here to Expand Forum to Full Width
|