How to resolve the algorithm Generic swap step by step in the BASIC programming language
How to resolve the algorithm Generic swap step by step in the BASIC programming language
Table of Contents
Problem Statement
Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in the given language, it is permissible that the two variables be constrained to having a mutually compatible type, such that each is permitted to hold the value previously stored in the other without a type violation. That is to say, solutions do not have to be capable of exchanging, say, a string and integer value, if the underlying storage locations are not attributed with types that permit such an exchange. Generic swap is a task which brings together a few separate issues in programming language semantics. Dynamically typed languages deal with values in a generic way quite readily, but do not necessarily make it easy to write a function to destructively swap two variables, because this requires indirection upon storage places or upon the syntax designating storage places. Functional languages, whether static or dynamic, do not necessarily allow a destructive operation such as swapping two variables regardless of their generic capabilities. Some static languages have difficulties with generic programming due to a lack of support for (Parametric Polymorphism). Do your best!
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Generic swap step by step in the BASIC programming language
Source code in the basic programming language
A=43:B=47:H=A:A=B:B=H:?" A="A" B="B;
x = 1
y$ = "hello"
SWAP x, y$
PRINT y$
global a, b
a = "one"
b = "two"
print a, b
call swap(a, b)
print a, b
end
subroutine swap(a, b)
temp = a : a = b : b = temp
end subroutine
a = 1.23 : b = 4.56
SWAP a,b
PRINT a,b
a$ = "Hello " : b$ = "world!"
SWAP a$,b$
PRINT a$,b$
a = 1.23 : b = 4.56
PROCswap(^a,^b, 5)
PRINT a,b
a$ = "Hello " : b$ = "world!"
PROCswap(^a$,^b$, 6)
PRINT a$,b$
END
DEF PROCswap(a%, b%, s%)
LOCAL i%
FOR i% = 0 TO s%-1
SWAP a%?i%,b%?i%
NEXT
ENDPROC
' FB 1.05.0
#Macro Declare_Swap(T)
Sub Swap_##T(ByRef t1 As T, ByRef t2 As T)
Dim temp As T = t2
t2 = t1
t1 = temp
End Sub
#EndMacro
Dim As Integer i, j
i = 1 : j = 2
Declare_Swap(Integer) ' expands the macro
Swap_Integer(i, j)
Print i, j
Dim As String s, t
s = "Hello" : t = "World"
Declare_Swap(String)
Swap_String(s, t)
Print s, t
Print
Print "Press any key to exit"
Sleep
window 1, @"Generic Swap", (0,0,480,270)
text ,,,,, 60
long i, j
double x, y
CFStringRef a, b
i = 1059 : j = 62
print i, j
swap i, j
print i, j
print
x = 1.23 : y = 4.56
print x, y
swap x, y
print x, y
print
a = @"Hello" : b = @"World!"
print a, b
swap a, b
print a, b
HandleEvents
Public Sub Main()
Dim vA As Variant = " World"
Dim vB As Variant = 1
Swap vA, vB
Print vA; vB
End
10 CLS : REM 10 HOME for Applesoft BASIC
20 A = 1 : B = 2
30 PRINT " A=";A," B=";B
40 T = A : A = B : B = T
50 PRINT " A=";A," B=";B
60 END
100 DEF SWAP(REF A,REF B)
110 LET T=A:LET A=B:LET B=T
120 END DEF
130 LET A=1:LET B=2
140 PRINT A,B
150 CALL SWAP(A,B)
160 PRINT A,B
10 LET A = 1
20 LET B = 2
30 PRINT " A=";A;" B=";B
40 LET T = A
50 LET A = B
60 LET B = T
70 PRINT " A=";A;" B=";B
80 END
macro Swap(a,b, c)
typeof(a) c
c=a
a=b
b=c
end macro
'demo with compound types:
'=========================
type point { float x,y}
point p={1,2}
point q={3,4}
swap p,q
print "p: " p.x "," p.y 'p: 3,4
print "q: " q.x "," q.y 'q: 1,2
Swap a, b
SUB nswap (a, b)
temp = a: a = b: b = temp
END SUB
a = 1
b = 2
PRINT a, b
CALL nswap(a, b)
PRINT a, b
a = 1
b = 2
'----- swap ----
tmp = a
a = b
b = tmp
end
Swap Var1, Var2
Define swap(swapvar1, swapvar2) = Prgm
Local swaptmp
#swapvar1 → swaptmp
#swapvar2 → #swapvar1
swaptmp → #swapvar2
EndPrgm
1 → x
2 → y
swap("x", "y")
x
2
y
1
LET a = 11
LET b = 22
PRINT a, " ", b
GOSUB 100
PRINT a, " ", b
END
100 REM swap(a, b)
LET t = a
LET a = b
LET b = t
RETURN
SUB swap(a, b)
LET temp = a
LET a = b
LET b = temp
END SUB
LET a = 1
LET b = 2
PRINT a, b
CALL swap(a, b)
PRINT a, b
END
a = 5 : b = 7
Print a,b
Push a,b : a = Pop() : b = Pop()
Print a,b
sub swap( byref x, byref y )
dim temp
temp = x
x = y
y = temp
end sub
dim a
a = "woof"
dim b
b = now()
swap a,b
wscript.echo a
wscript.echo b
5/02/2010 2:35:36 PM
woof
Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Dim temp = a
a = b
b = temp
End Sub
Dim a = 1, b = 2
Swap(a, b)
PROGRAM "nswap"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
a = 1
b = 2
PRINT a, b
GOSUB nswap
PRINT a, b
SUB nswap
temp = a: a = b: b = temp
END SUB
END FUNCTION
END PROGRAM
a = 1
b = 2
print a, b
//----- swap ----
temp = a : a = b : b = temp
print a, b
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the J programming language
You may also check:How to resolve the algorithm String prepend step by step in the OCaml programming language
You may also check:How to resolve the algorithm A+B step by step in the Julia programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Salmon programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the Go programming language