Hello,
The code below is a PB version of C/C++ code found at http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
The problem is that the code below produce the same string for each call to permute.
Could someone shed some light on what is wrong? Thank you!
--
#Compiler PBCC 6.04
#Compile Exe
#Dim All
#Register None
'%Debug = 1&
'#Include Once "Debug.bi"
%USEMACROS = 1
#Include Once "Win32Api.inc"
'------------------------------------------------------------------------------
Function PBMain () As Long
Local s As StringZ * 4
s = "ABC"
permute(s, 0, Len(s)-1)
? "Press a key to end the program..."
WaitKey$
End Function
'----------------------------------------------------------------------------
'/* Function to print permutations of string
' This function takes three parameters:
' 1. String
' 2. Starting index of the string
' 3. Ending index of the string. */
Sub permute(a As StringZ,ByVal n As long,ByVal r As long)
Local s1 As StringZ Ptr ' * 4
Local s2 As StringZ Ptr ' * 4
Local i As Byte
s1 = VarPtr(a)
s2 = s1
If n = r Then
? @s1
Else
For i = n To r
s1 = s1+n
s2 = s2+i
Swap s1,s2
Call permute(a,n+1,r)
s1 = s2+n
s2 = s2+i
Swap s1,s2 '; //backtrack
Next
End If
End Sub
Works now. Has anyone any clue to why the previous version didn't produce correct result?
--
#Compiler PBCC 6.04
#Compile Exe
#Dim All
#Register None
%Debug = 1&
'#Include Once "Debug.bi"
%USEMACROS = 1
#Include Once "Win32Api.inc"
'------------------------------------------------------------------------------
Function PBMain () As Long
Local s As String
s = "ABC"
permute(s, 0, Len(s)-1)
? "Please, press a key to end the program..."
WaitKey$
End Function
'/* Function to print permutations of string
' This function takes three parameters:
' 1. String
' 2. Starting index of the string
' 3. Ending index of the string. */
Sub permute(ByVal a As String,ByVal n As Long,ByVal r As Long)
Local i As Long
Local s1 As Byte Ptr
s1 = StrPtr(a)
If n = r Then
? a
Else
For i = n To r
Swap @s1[n],@s1[i] 'a+n,a+i
Call permute(a,n+1,r)
Swap @s1[n],@s1[i] 'a+n,a+i) '; //backtrack
Next
End If
End Sub
I'll look at it in just a bit Olav.
Well, this is a bit tricky...
void swap(char* x, char* y) /* Function to swap values at two pointers */
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
The function parameters are two char* (character pointers), which can point to arrays of chars containing from zero to 2^31 characters. But note the local char temp variable is not a pointer to anything, but simply is a C variable type capable of only containing one single character. When they make this assignment...
temp = *x;
...temp will only contain the 1st character of whatever char pointer variable x points to, e.g., if x points to this string...
"ABC"
...then temp will contain just the 'A' character and nothing more.
I took a quick look at your 2nd version which you say works, and I see your are using pointer offset notation to extract just the single character. The closest thing in PowerBASIC to the C/C++ char is the Byte.