Multi-Dimensional Arrays are two or more arrays that are combined into one. Some examples of a situation where a multi-dimensional array might be used would be a chess board, grid, or a deck of cards.


For this tutorial we will create a multi-dimensional array for a deck of cards.


In a deck of cards, there are 4 suits and 13 ranks per suit.

The suits are (Diamonds, Spades, Hearts, Clubs)

The ranks are (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace)


We can create a multi-dimensional array from an array of suits and an array of ranks.


This is the code to create a multi-dimensional array for a deck of cards:


program Project1;


{$mode objfpc}{$H+}


uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


var

  x:integer;

  y:integer;

  suit:array[1..4] of string;

  rank:array[1..13] of string;

  cards:array[1..4,1..13] of string; {This is the Multi-Dimensional Array}


begin

  suit[1] := 'D'; {Diamonds}

  suit[2] := 'S'; {Spades}

  suit[3] := 'H'; {Hearts}

  suit[4] := 'C'; {Clubs}


  rank[1] := '2';

  rank[2] := '3';

  rank[3] := '4';

  rank[4] := '5';

  rank[5] := '6';

  rank[6] := '7';

  rank[7] := '8';

  rank[8] := '9';

  rank[9] := '10';

  rank[10] := 'Jack';

  rank[11] := 'Queen';

  rank[12] := 'King';

  rank[13] := 'Ace';


  for x := 1 to 4 do

  begin

    for y := 1 to 13 do

    begin

      cards[x,y] := suit[x] + rank[y];

    end;

  end;


  for x := 1 to 4 do

  begin

    for y := 1 to 13 do

    begin

      write(cards[x,y], ' ');

    end;

  end;


  writeln; writeln;

  writeln('Press <Enter> To Quit');

  readln;


end.




Video Tutorial: Free Pascal Tutorial 10 - Multi-Dimensional Arrays - Lazarus (8:30)

Previous                   Next