How can I do in C?
It's about mapping function matrix. The two-dimensional mapping function is:
fm(i,j) = Eb + t*[(i-1)*n +j-1]
Algorithm creates an array data type with the specified function, knowing that proc (procedure) to pass the parameter by reference.
Algorithm:
TAD matrix def
rep = reg( val:vet[1...max] of value; "vector for the elements"
eb: int; "address (cursor) initial"
t: int; "size of the elements"
d: int; "number of dimensions"
L1,L2: int) "limits of rows and columns"
op = proc create_twodim(M: matrix; L1, L2: int);
proc attribution(M: matrix; i, j: int; v: value);
func query(M: matrix; i, j: int) return value;
func row_dim(M: matrix, k: int) return int;
end matrix;
TAD matrix implem;
func fm(i, j: int) return int;
"Dimensional mapping function"
begin
return M.eb +M.t*[(i-1)*M.L2 + j-1]
end;
func row_dim(M: matrix, k: int) return int;
"Provides the limit of one dimension of the matrix M"
begin
if k = 1
then return M.L1
else of k = 2
then return M.L2
else error("Invalid size!")
end;
proc create_twodim(M: matrix; L1, L2: int);
"Sets the contents of the two-dimensional descriptor"
begin
M.eb := 1;
M.t := 1;
M.d := 2;
M.L1 := L1;
M.L2 := L2;
end;
proc attribution(M: matrix; i, j: int; v: value);
"Associates the value v to the indices i, j in M"
begin
if i <= M.L1 & j <= M.L2
then M.val[ fm(i,j) ] := v;
else error("Indexes invalid!");
end;
func query(M: matrix; i, j: int) return value;
"Provides the value associated with indices i, j in M"
begin
if 0 < i <= M.L1 & 0 < j <= M.L2
then return M.val[ fm(i,j) ]
else error("Indexes invalid!");
end;
end matrix