program CalcolaDeterminante;
const
NMAX = 4;
type
TipoArrayMatrice = array [1..NMAX, 1..NMAX] of integer;
TipoMatrice = record
dim : 0..NMAX;
mat : TipoArrayMatrice;
end;
function Determinante (matrice: TipoMatrice): integer;
procedure Minore (j : integer;
matrice_in : TipoMatrice;
var matrice_out : TipoMatrice);
var
riga, colonna_in, colonna_out : integer;
begin
matrice_out.dim := matrice_in.dim - 1;
for riga := 2 to matrice_in.dim do
begin
colonna_out := 1;
for colonna_in := 1 to matrice_in.dim do
begin
if colonna_in <> j then
begin
matrice_out.mat[riga-1,colonna_out]:= matrice_in.mat[riga,colonna_in];
colonna_out := colonna_out + 1
end
end
end
end;
function Coefficiente (j: integer): integer;
begin
if odd(j) then
Coefficiente := 1
else
Coefficiente := -1
end;
var
somma, i : integer;
matrice_aux : TipoMatrice;
begin
if matrice.dim = 1 then
Determinante := matrice.mat[1,1]
else if matrice.dim = 2 then
Determinante := matrice.mat[1,1] * matrice.mat[2,2] -
matrice.mat[1,2] * matrice.mat[2,1]
else
begin
somma := 0;
for i := 1 to matrice.dim do
begin
Minore(i, matrice, matrice_aux);
somma := somma +
Coefficiente(i) * matrice.mat[1,i] * Determinante(matrice_aux)
end;
Determinante := somma
end
end;
procedure LeggiMatrice (var matrice: TipoMatrice);
var
nome : string;
f : text;
i, j : integer;
begin
write('Nome del file dal quale leggere la matrice ? ');
readln(nome);
assign(f, nome);
reset(f);
read(f, matrice.dim);
if matrice.dim > NMAX then
begin
writeln('Dimensione matrice troppo grande');
matrice.dim := 0
end
else
for i := 1 to matrice.dim do
for j := 1 to matrice.dim do
read(f, matrice.mat[i,j]);
close(f)
end;
procedure StampaMatrice (matrice: TipoMatrice);
var
i, j : integer;
begin
for i := 1 to matrice.dim do
begin
for j := 1 to matrice.dim do
write(matrice.mat[i,j]:7);
writeln
end;
writeln
end;
var
m : TipoMatrice;
begin
LeggiMatrice(m);
writeln('Ho letto la matrice:');
StampaMatrice(m);
writeln('Il suo determinante vale: ', Determinante(m));
end.