[Gambas-user] Recursion does not work
Demosthenes Koptsis
demosthenesk at ...626...
Thu Jan 21 16:24:09 CET 2010
Hi,
i use ubuntu 9.10 and Gambas 2.13
i create an anagram application port from phpAG
http://phpag.sourceforge.net/
i completed the same app with VB5 and now i make it also in Gmabas
To make anagrams i use recursion
i face up with a problem of recursion.
i call anag() function from
PUBLIC SUB Button1_Click()
....
anag(0, iLen, sArray) 'Make anagrams
....
END
And the function is this
PRIVATE FUNCTION anag(iStart AS Integer, iLen AS Integer, sArray AS Array)
'vars are ByVal by default
'Port code from phpAG code
' / * FOR ($j = $start; $j < $len; + + $j)
' {
' IF ($j == $start || $array[$j] != $array[$start])
' {
' $tmp = $array[$j];
' $array[$j] = $array[$start];
' $array[$start] = $tmp;
' anag($array, $start + 1, $len);
' }
' }
' * /
DIM j AS Integer
DIM tmp AS String
DIM word AS String
IF iStart < iLen THEN
j = iStart
WHILE (j < iLen)
IF (j = iStart OR sArray[j] <> sArray[iStart]) THEN
tmp = sArray[j]
sArray[j] = sArray[iStart]
sArray[iStart] = tmp
anag((iStart + 1), iLen, sArray) 'recursion does not work
properly
END IF
j = j + 1
WEND
ELSE
word = implode(sArray)
TextArea1.Text = TextArea1.Text & word & gb.NewLine
'Vb5 PRINT #2, word 'write to anagram.tmp the result
'
END IF
END
PRIVATE FUNCTION implode(sArray AS Array) AS String
'This function makes a string from the elements of a string array
DIM i AS Integer
DIM word AS String
FOR i = 0 TO (sArray.Length - 1)
word = word & sArray[i]
NEXT
RETURN word
END
---------------
So if user enter the letters "abc" must have all the combinations as
abc
acb
cab
cba
bca
bac
The same code works for vb5 like this
Private Function anag(ByVal iStart As Integer, ByVal iLen As Integer, ByVal
sArray As Variant)
'Port code from phpAG
If iStart < iLen Then
j = iStart
While (j < iLen)
If j = iStart Or sArray(j) <> sArray(iStart) Then
tmp = sArray(j)
sArray(j) = sArray(iStart)
sArray(iStart) = tmp
Call anag((iStart + 1), iLen, sArray)
End If
j = j + 1
Wend
Else
word = implode(sArray)
Text2.Text = Text2.Text & word & vbNewLine
Print #2, word 'write to anagram.tmp the result
End If
End Function
in Gambas i get wrong results i get
abc
acb
cab
cba
abc
acb
This means recursion does not work properly.
In VB5 it was needed to pass values BYVAL i read that this is the defaut in
Gambas. But does not work.
Why?
i attach my program for whoever want to help.
--
Γεια χαρα σε όλους!!!
Regards,
Demosthenes Koptsis
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Anagramatismos.tar.gz
Type: application/x-gzip
Size: 11329 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20100121/9c8f8584/attachment.bin>
More information about the User
mailing list