Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Rewriting C/ C++ code in Pascal

Tags:
  1. Dec 14, 2015 #1
    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 (Text):

    #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)
     
     
  2. jcsd
  3. Dec 14, 2015 #2

    Mark44

    Staff: Mentor

    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.
     
  4. Dec 14, 2015 #3
    Yes, I am an idiot.
    So far I have done this but the swap function is not working properly.. Not sure why.

    Code (Text):
    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.
     
  5. Dec 14, 2015 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Dec 14, 2015 #5

    jtbell

    User Avatar

    Staff: Mentor

    What exactly is it supposed to do, and what is it actually doing?
     
  7. Dec 14, 2015 #6
    It should do this:
    http://d1gjlxt8vb0knt.cloudfront.net//wp-content/uploads/NewPermutation.gif [Broken]

    But for some reason the string suddenly starts to elongale, shorten...
     
    Last edited by a moderator: May 7, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Rewriting C/ C++ code in Pascal
  1. Critique my C++ code (Replies: 11)

Loading...