Rewriting C/ C++ code in Pascal

  • Context: C/C++ 
  • Thread starter Thread starter tawi
  • Start date Start date
  • Tags Tags
    C++ Code Pascal
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 2K views
tawi
Messages
33
Reaction score
0
Hello, I have a code in C/ C++and I have just learned I am going to need it in a version compatible with Free Pascal. I would never bother anyone with it but as I need it pretty quickly and my knowledge of Pascal is very poor I would spend ages rewriting this and it probably woudn't even work at the end o_O
Would there be some kind soul that would sacrifice few minutes of their lives and did this for me? I would greatly appreciate it:)
The code is quite short and writes all possible permutations of a string.

Code:
#function to swap values
def swap(a,l,r):
  t = A[l]
  A[l] = A[r]
  A[r] = t
  return A
def toList(string):
  List = []
  for x in string:
  List.Append(x)
  return List
def toString(List):
  return ''.join(List)

#funtion to print permutations with three parameters: string, starting index of the string and ending index

def permute(A, l, r):
  if l==r:
  print toString(A)
  else:
  for i in xrAnge(l,r+1):
  A = swAp(A,l,i)
  permute(A, l+1, r)
  A = swAp(A,l,i) # bAcktrAck

string = "ABC"
n = len(string)
A = toList(string)
permute(A, 0, n-1)

 
on Phys.org
tawi said:
Hello, I have a code in C/ C++and I have just learned I am going to need it in a version compatible with Free Pascal. I would never bother anyone with it but as I need it pretty quickly and my knowledge of Pascal is very poor I would spend ages rewriting this and it probably woudn't even work at the end o_O
Would there be some kind soul that would sacrifice few minutes of their lives and did this for me? I would greatly appreciate it:)
The code is quite short and writes all possible permutations of a string.

Code:
#function to swap values
def swap(a,l,r):
  t = A[l]
  A[l] = A[r]
  A[r] = t
  return A
def toList(string):
  List = []
  for x in string:
  List.Append(x)
  return List
def toString(List):
  return ''.join(List)

#funtion to print permutations with three parameters: string, starting index of the string and ending index

def permute(A, l, r):
  if l==r:
  print toString(A)
  else:
  for i in xrAnge(l,r+1):
  A = swAp(A,l,i)
  permute(A, l+1, r)
  A = swAp(A,l,i) # bAcktrAck

string = "ABC"
n = len(string)
A = toList(string)
permute(A, 0, n-1)
Your code is not C or C++ -- it's Python.

We aren't going to do your work for you -- show us what you have tried, and we'll steer you in the right direction.
 
Yes, I am an idiot.
So far I have done this but the swap function is not working properly.. Not sure why.

Code:
permu(output);

function swap(str: String; firstIndex: Integer; secondIndex: Integer): String;
var
tmp : Char;
begin
    tmp:=str[firstIndex];
    str[firstIndex]:=str[secondIndex];
    str[secondIndex]:=tmp;
 
    writeln('SWAP str ',str);
    swap:=str;
end;

function permute(str: String; startIndex: Integer; endIndex: Integer): Integer;
var
i : Integer;

begin
    if startIndex = endIndex then
    begin
        writeln(str);
    end
    else
        begin
            for i := startIndex to endIndex do
                begin
                    str:=swap(str,startIndex,i);
                    permute(str, startIndex+1, endIndex);                   
                    str:=swap(str,startIndex, i);
                end;
        end;
end;

var
str : String;
L : Integer;

begin
    readln(str);
    L := length(str);
    permute(str,0,L-1);

end.
 
Your swap function looks like it would work. When you call it the first time, what string goes into the function? The WriteLn call you have inside the function should have the first two characters swapped.
 
It should do this:
http://d1gjlxt8vb0knt.cloudfront.net//wp-content/uploads/NewPermutation.gif

But for some reason the string suddenly starts to elongale, shorten...
 
Last edited by a moderator: