6

Example 2: Multi-Dimension Array

The Pascal procedure, RealCA.p. Pascal passes low bound, high bound, and element width.

procedure RealCA(var A: array [r1..r2: integer] of array [c1..c2: integer] of real);

var

col, row: integer; begin

for row := r1 to r2 do for col := c1 to c2 do

if row = col then A[row, col] := 1.0

else

A[row, col] := 0.0

end; { RealCA }

The C main program, RealCAMain.c. Array M has 2 rows of 3 columns each. c1 and c2 are the first and last columns. r1 and r2 are the first and last rows. wc is the width of a column element (smallest) and is equal to sizeof( M[0][0] ). wr is the width of a row element (next largest) and is equal to (c2-c1+1) * wc.

#include <stdio.h>

#define NC 3 #define NR 2

extern void RealCA(double [][NC], int, int, int, int, int);

int main(void)

 

{

 

 

 

double

M[NR][NC];

int

 

 

col, c1, c2, row, r1, r2, wc, wr;

c1

=

0;

 

r1

=

0;

 

c2

=

NC - 1;

 

r2

=

NR - 1;

 

wc =

sizeof(M[0][0]);

wr =

(c2 - c1 + 1) * wc;

RealCA(M, r1, r2, wr, c1, c2);

for (row = r1; row <= r2; row++) { printf("\n");

for (col = c1; col <= c2; col++) printf("%4.1f", M[row][col]);

};

printf("\n");

}

The C–Pascal Interface

103