Hello, i have some problems implementing the following pseudo-code. Te problem is that in Pascal i can return user defined data structures (in this case: matrix), and i also tried to return it as a parameter with "var" but it gave a stack overflow error. Anyone can help me?
pseudo (the well known matrix chain multiplication algorithm):
MCM (X,s,i,j)
if j>i then m1=MCM(X,s,i,s[i,j])
m2=MCM(X,s,s[i,j]+1,j)
return MatMult(m1,m2)
else return X(i)
X - sequence of matrixes
s - split table
MatMult - multiplies 2 matrixes (i wrote it so that it stores the result in a third one like MatMult(m1,m2,m3)
my pascal implementation:
procedure MCM(x1:seqmat; s1:matrix; i1,j1:integer; var res:matrix);
var a,b,c:matrix;
begin
if j1>i1 then begin
MCM(x1,s1,i1,s1.matr[i1,j1],a);
MCM(x1,s1,s1.matr[i1,j1+1],j1,b);
MatMult(a,b,res);
end
else res:=x1[i1];
end;