faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 14 people (93%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Delphi: Array: 2D: Print: Simple: How print a 2-dimensional array? [parameter pass procedure]

Nov 14th, 2003 08:53
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 14 November 2003 - 05:39 pm -------------------

Delphi: Array: 2D: Print: Simple: How print a 2-dimensional array? 
[parameter pass procedure]

---

Steps: Overview:

 1. -Create a new application in Delphi

 2. -Put a button on the form

 3. -Double click on the button

 4. -As data e.g. the following is given.
     The 2-dimensional array with its data

       [0,0] = 0
       [0,1] = 1
       [0,2] = 2
       [1,0] = 3
       [1,1] = 4
       [1,2] = 5
       [2,0] = 6
       [2,1] = 7
       [2,2] = 8

 5. -To print use e.g. a linear print
     of all the elements of the array

      Thus in pseudocode:

        for row = rowfirst to rowlast
         print row
        end

     To print the rows, you might
     print a newline character,
     so that each row is printed
     on a seperate line.

     The newline character in Delphi
     is a #13 followed by #10.
     Thus altogether #13#10.

 6. -Thus this might become in Delphi:

var i : integer;

var j : integer;

begin

 for i := Low( myarray ) to High( myarray ) do begin

  for j := Low( myarray[ i ] ) to High( myarray[ i ] ) do begin

   if ( myarray[ i, j ] ) = integerprintI then begin

    ShowMessage( Format( '[ %d, %d ] -> %d', [ i, j, myarray[ i, 
j ] ] ) );

   end;

  end;

 end;

 6. -So all together this might give the following source code:

--- cut here ---------------------------------------------------------

// --- MAIN --- //

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,

  Dialogs, StdCtrls;

type

 Tmyarray = array[0..2, 0..2] of integer;

type

  TForm1 = class(TForm)

    Button1: TButton;

// --- FUNCTION: HEADER: BEGIN --- //

    procedure Button1Click(Sender: TObject);

    procedure PROCArrayInitializePrintElement( var myarray : 
Tmyarray );

    procedure PROCArrayPrintElementMain;

    procedure PROCArrayViewPrintElement( integerprintI : integer; 
myarray : Tmyarray );

// --- FUNCTION: HEADER: END --- //

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

implementation

{$R *.dfm}

// Set range checking on

{$R+}

procedure TForm1.Button1Click(Sender: TObject);

begin

 PROCArrayPrintElementMain;

end;

// --- LIBRARY --- //

// library: array: view: print: element [kn, ni, fr, 14-11-2003 
15:14:22]

procedure TForm1.PROCArrayInitializePrintElement( var myarray : 
Tmyarray );

begin

 myarray[ 0, 0 ] := 0;

 myarray[ 0, 1 ] := 1;

 myarray[ 0, 2 ] := 2;

 myarray[ 1, 0 ] := 3;

 myarray[ 1, 1 ] := 4;

 myarray[ 1, 2 ] := 5;

 myarray[ 2, 0 ] := 6;

 myarray[ 2, 1 ] := 7;

 myarray[ 2, 2 ] := 8;

end;

// library: array: view: print: element [kn, ni, fr, 14-11-2003 
15:14:22]

procedure TForm1.PROCArrayPrintElementMain;

 // e.g. // Set range checking on

 // e.g. {$R+}

 // e.g.

 // e.g. type

 // e.g.  Tmyarray = array[0..2, 0..2] of integer;

 // e.g.

 // e.g. PROCArrayPrintElementMain;

var myarray : Tmyarray;

begin

 PROCArrayInitializePrintElement( myarray );

 PROCArrayViewPrintElement( 4, myarray );

end;

// library: array: view: print: element [kn, ni, fr, 14-11-2003 
15:14:22]

procedure TForm1.PROCArrayViewPrintElement( integerprintI : integer; 
myarray : Tmyarray );

 // e.g. type

 // e.g.  Tmyarray = array[0..2, 0..2] of integer;

 // e.g.

 // e.g. var myarray : Tmyarray;

 // e.g. PROCArrayViewPrintElement( 4, myarray );

var i : integer;

var j : integer;

var s : string;

begin

 s := '';

 for i := Low( myarray ) to High( myarray ) do begin

  for j := Low( myarray[ i ] ) to High( myarray[ i ] ) do begin

   s := s + Format( '[ %d ]', [ myarray[ i, j ] ] );

  end;

  s := s + #13#10;

 end;

 ShowMessage( s );

end;

end.

--- cut here ---------------------------------------------------------

 6. -If you run this program, you will see a message box showing:

  +---------------+
  |               |
  |  [0] [1] [2]  |
  |  [3] [4] [5]  |
  |  [6] [7] [8]  |
  |               |
  |   +------+    |
  |   |  OK  |    |
  |   +------+    |
  |               |
  +---------------+

---
---

Internet: see also:

---

Delphi: Array: 1D: Print: Simple: How print in a 1-dimensional array? 
[parameter pass procedure]
http://www.faqts.com/knowledge_base/view.phtml/aid/26487/fid/175

---

Delphi:Datastructure:Array:Multidimensional:Procedure: How pass 
multidimensional array as parameter?
http://www.faqts.com/knowledge_base/view.phtml/aid/26484/fid/175

----------------------------------------------------------------------