Results 1 to 5 of 5

Thread: Passing array to function

  1. #1
    Join Date
    Nov 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Passing array to function

    Well I picked up scar again after a bit of other work in c. I decided to tackel bejeweled and have come quite far.

    One hindrance is that I am completely unaware for how to pass an array to a function. Let me give you an example.

    Code:
    var
    PixelArray : array[1..8] of array[1..8] of Rectangle; // Coords of gems
    GemArray : array[1..8] of array[1..8] of Integer; //Colors of Gems
    Patterns : array[1..3] of array [1..3] of array[1..24] of Integer; //Stores 24 3x3 Patterns
    RedMask : array[1..8] of array[1..8] of Integer; //Red Mask of Board
    This is where I declare my arrays. I need to throw those around different places in my program. I also would like to have functions that don't have to be hard-coded for a particular array. Something like...

    Code:
    Procedure PrintGemArray(a: array[1..8] of array[1..8] of Integer);
    var i : Integer;
    begin
    for i := 1 to 8 do
    writeln(inttostr(a[i][1]) + ' ' + inttostr(a[i][2]) + ' ' + inttostr(a[i][3]) + ' ' + inttostr(a[i][4]) + ' ' + inttostr(a[i][5]) + ' ' + inttostr(a[i][6]) + ' ' + inttostr(a[i][7]) + ' ' + inttostr(a[i][8]));
    end;
    Unfortunately, this does not work or so it seems. After searching like mad about pascal arrays on google and in here, I found that arrays passed to functions cannot be static. So I tried..

    Code:
    RedMask : array of array of Integer; //Red Mask of Board
    Code:
    Procedure PrintGemArray(a: array of array of Integer);
    var i : Integer;
    begin
    for i := 1 to 8 do
    writeln(inttostr(a[i][1]) + ' ' + inttostr(a[i][2]) + ' ' + inttostr(a[i][3]) + ' ' + inttostr(a[i][4]) + ' ' + inttostr(a[i][5]) + ' ' + inttostr(a[i][6]) + ' ' + inttostr(a[i][7]) + ' ' + inttostr(a[i][8]));
    end;
    This at least gets me into the function, but I get a out of range error on the writeln() part. I'm assuming I have to somehow set the length without making it static, so that it can still be passed to the function?

    Any help or pointers would be appreciated. It's very frustrating not being able to move my data around.

    Thanks!

    Ransom

  2. #2
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Did you set the length of 'a'?

  3. #3
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    var
      myArr: T2DIntArray;
    // or array of array of Integer;
      i: Integer;
    begin
      SetLength(myArr, 9); // This would be like array [0..8] of array of Integer;
      for i := 0 to High(myArr) do // Go through all indexes, so 0, 1 .. 8
        SetLength(myArr[i], 9); // This sets each array to be like array [0..8] of
      // myArr now is the same as array [0..8] of array [0..8] of Integer;
    end;

    SetLength sets the length of the array - dynamic arrays always start on 0, with their highest index being the length you set it minus 1 though, hence the arrays being the same as [0..8] when being set to a length of 9.
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  4. #4
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Just For you:
    SCAR Code:
    Procedure PrintGemArray(a: array of array of Integer);
    var i : Integer;
    begin
    SetLength(a, 9);
      for i := 1 to 8 do
      Begin  
        SetLength(a[i], 9);
        writeln(inttostr(a[i][1]) + ' ' + inttostr(a[i][2]) + ' ' + inttostr(a[i][3]) + ' ' + inttostr(a[i][4]) + ' ' + inttostr(a[i][5]) + ' ' + inttostr(a[i][6]) + ' ' + inttostr(a[i][7]) + ' ' + inttostr(a[i][8]));
      End;
    end;

  5. #5
    Join Date
    Nov 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks a ton guys. This is working for my example and I'm currently changing the rest of my arrays. This will now go much smoother.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •