Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

Determinanta n-tog reda, ako moze neko da pogleda kod...

Determinanta n-tog reda, ako moze neko da pogleda kod... 03.01.2007. u 20:33 · pre 242 meseca #1
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!
Potpis
And don't be mad at me for crying for humanity,
call it pretensions but I got good intentions,
to keep my sanity, I'm hoping, fuck, there's gotta be
an answer to their strategies and their evil inventions... ~Looptroop
Re: Determinanta n-tog reda, ako moze neko da pogleda kod... 04.01.2007. u 17:00 · pre 242 meseca #2
My bad...
Funkcija ne vraca real rezultat za determinante viseg reda, nego se za njih ponasa kao procedura...
Potpis
And don't be mad at me for crying for humanity,
call it pretensions but I got good intentions,
to keep my sanity, I'm hoping, fuck, there's gotta be
an answer to their strategies and their evil inventions... ~Looptroop
Re: Determinanta n-tog reda, ako moze neko da pogleda kod... 04.01.2007. u 17:25 · pre 242 meseca #3
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.
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme