Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

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

[es] :: Art of Programming :: Determinanta n-tog reda, ako moze neko da pogleda kod...

[ Pregleda: 978 | Odgovora: 2 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Divjak
Vladimir Divjak

Član broj: 4783
Poruke: 535
*.zrenjanin.beotel.net.



Profil

icon Determinanta n-tog reda, ako moze neko da pogleda kod...03.01.2007. u 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!
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
03.01.2007. u 19:33 

Divjak
Vladimir Divjak

Član broj: 4783
Poruke: 535
*.zrenjanin.beotel.net.



Profil

icon Re: Determinanta n-tog reda, ako moze neko da pogleda kod...04.01.2007. u 16:00
My bad...
Funkcija ne vraca real rezultat za determinante viseg reda, nego se za njih ponasa kao procedura...

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
04.01.2007. u 16:00 

djoka_l
Beograd

Član broj: 56075
Poruke: 291
*.antegra.com.



Profil

icon Re: Determinanta n-tog reda, ako moze neko da pogleda kod...04.01.2007. u 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.
04.01.2007. u 16:25 

[es] :: Art of Programming :: Determinanta n-tog reda, ako moze neko da pogleda kod...

[ Pregleda: 978 | Odgovora: 2 ]

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.