[ Divjak @ 03.01.2007. 19:33 ] @
Kod radi kako bi trebalo medjutim kad dodje do dela
Code:

Det := Det + sgn*Compute(k,1,depth-1,Mat2);

a depth je 2, pravilno izracuna taj deo i zatim po izlasku iz procedure, umesto da predje na sledeci red, javi mi SIGFPE Arithmetic Exception... Zasto?
Inace, radim u Free Pascalu...

Code:

Program Domaci;
Const
  MaxIndex = 100;
Type
  Matrica = Array[1..MaxIndex,1..MaxIndex] Of Real;
Var
  Mat1 : Matrica;
  n : Integer;
  Det : Real;

Procedure Load;
Var
  i,j : Integer;
  Fin : Text;
Begin
  Assign(Fin,'Det.dat');
  Reset(Fin);
  Read (n);
  Det := 0;
  Writeln;
  For i:=1 to n Do
    For j:=1 to n Do Begin
      Read(Fin,Mat1[i,j]);
    End;
    Close(Fin);
End;

Procedure Draw;
Var
  i,j : integer;
Begin
  For i:=1 to n Do Begin
    For j:=1 to n Do Begin
      Write(Mat1[i,j]:0:0,'|');
    End;
  Writeln;
  End;
End;

Function Compute(a,b,depth : Integer;Mat : Matrica) : Real;
var
  m,l,k,i,j : Integer;
  NextRow : Boolean;
  a1,sgn : Integer;
  Mat2 : Matrica;
Begin
  If depth=2 then Begin
    Det:=Det+Mat[a,b]*Mat[a+1,b+1]-(Mat[a,b+1]*Mat[a+1,b]);
  End
  Else Begin
  For k := 1 to depth do Begin
    m:=1;
    l:=1;
    For i:=1 to depth Do Begin
    NextRow:=False;
      For j:=1 to depth Do Begin
        If (not(i=k)) and (not(j=b)) Then Begin
          Mat2[m,l]:=Mat[i,j];
          inc(l);
          NextRow:=True;
        End;
      End;
      If NextRow then inc(m);
      l:=1;
    End;
    if (k+b) mod 2 = 0 then sgn:=1 else sgn:=-1;
    Det := Det + sgn*Compute(k,1,depth-1,Mat2);
    End;
  End;
End;

Begin
  Load;
  Draw;
  Writeln;
  Compute(1,1,n,Mat1);
  Writeln(Det);
End.



Molim vas, pomozite!
Hvala!
[ Divjak @ 04.01.2007. 16:00 ] @
My bad...
Funkcija ne vraca real rezultat za determinante viseg reda, nego se za njih ponasa kao procedura...
[ djoka_l @ 04.01.2007. 16:25 ] @
Opet školski primer zloupotrebe rekurzije!

Determinante se NE RAČUNAJU REKURZIJOM! Isto kao što se sistemi linearnih jednačina ne računaju preko determinanti.
Treba je svesti na trougaonu pa je onda Det proizvod elemenata na glavnoj dijagonali.