home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l042 / 1.ddi / CHAP6.ARC / DET.PAS next >
Encoding:
Pascal/Delphi Source File  |  1987-12-30  |  5.2 KB  |  176 lines

  1. program Determinant_Prog;
  2.  
  3. {--------------------------------------------------------------------------}
  4. {-                                                                        -}
  5. {-     Turbo Pascal Numerical Methods Toolbox                             -}
  6. {-     Copyright (c) 1986, 87 by Borland International, Inc.              -}
  7. {-                                                                        -}
  8. {-       Purpose : this program uses the routine Determinant to find the  -}
  9. {-                 determinant of a matrix.                               -}
  10. {-                                                                        -}
  11. {-       Unit    : Matrix    procedure Determinant                        -}
  12. {-                                                                        -}
  13. {--------------------------------------------------------------------------}
  14.  
  15. {$R+}    { Enable range checking }
  16. {$I-}    { Disable I/O checking }
  17.  
  18. uses
  19.   Matrix, Dos, Crt, Common;
  20.  
  21. var
  22.   Dimen : integer;    { Size of the square matrix }
  23.   Data : TNmatrix;    { The matrix }
  24.   Det : Float;        { The determinant of Data }
  25.   Error : byte;       { Flags if something went wrong }
  26.  
  27. procedure Initial(var Dimen : integer;
  28.                   var Data  : TNmatrix);
  29.  
  30. {----------------------------------------------------------}
  31. {- Output: Dimen, Data                                    -}
  32. {-                                                        -}
  33. {- This procedure intializes the above variables to zero. -}
  34. {----------------------------------------------------------}
  35.  
  36. begin
  37.   Dimen := 0;
  38.   FillChar(Data, SizeOf(Data), 0);
  39. end; { procedure Initial }
  40.  
  41. procedure GetData(var Dimen : integer;
  42.                   var Data  : TNmatrix);
  43.  
  44. {---------------------------------------------------}
  45. {- Output: Dimen, Data                             -}
  46. {-                                                 -}
  47. {- This procedure sets the value of Dimen and Data -}
  48. {- from either keyboard input or file input        -}
  49. {---------------------------------------------------}
  50.  
  51. var
  52.   Ch : char;
  53.  
  54. procedure GetDataFromKeyboard(var Dimen : integer;
  55.                               var Data  : TNmatrix);
  56.  
  57. {--------------------------------------}
  58. {- Output: Dimen, Data                -}
  59. {-                                    -}
  60. {- This procedure sets the value of   -}
  61. {- Dimen and Data from keyboard input -}
  62. {--------------------------------------}
  63.  
  64. var
  65.   Row, Column : integer;
  66.  
  67. begin
  68.   Writeln;
  69.   repeat
  70.     Write('Dimension of the square matrix (1-',TNArraySize,')? ');
  71.     Readln(Dimen);
  72.     IOCheck;
  73.   until (not IOerr) and (Dimen >= 1) and (Dimen <= TNArraySize);
  74.   Writeln;
  75.   for Row := 1 to Dimen do
  76.     for Column := 1 to Dimen do
  77.       repeat
  78.         Write('Matrix[', Row, ', ', Column, ']: ');
  79.         Readln(Data[Row, Column]);
  80.         IOCheck;
  81.       until not IOerr;
  82. end; { procedure GetDataFromKeyboard }
  83.  
  84. procedure GetDataFromFile(var Dimen : integer;
  85.                           var Data  : TNmatrix);
  86.  
  87. {--------------------------------------}
  88. {- Output : Dimen, Data               -}
  89. {-                                    -}
  90. {- This procedure sets the value of   -}
  91. {- Dimen and Data from file input     -}
  92. {--------------------------------------}
  93.  
  94. var
  95.   FileName : string[255];
  96.   InFile : text;
  97.   Row, Column : integer;
  98.  
  99. begin
  100.   Writeln;
  101.   repeat
  102.     Writeln;
  103.     repeat
  104.       Write('File name? ');
  105.       Readln(FileName);
  106.       Assign(InFile, FileName);
  107.       Reset(InFile);
  108.       IOCheck;
  109.     until not IOerr;
  110.     Read(InFile, Dimen);
  111.     IOCheck;
  112.     Row := 0;
  113.     while (not IOerr) and (Row < Dimen) do
  114.     begin
  115.       Row := Succ(Row);
  116.       Column := 0;
  117.       while (not IOerr) and (Column < Dimen) do
  118.       begin
  119.         Column := Succ(Column);
  120.         Read(InFile, Data[Row, Column]);
  121.         IOCheck;
  122.       end;
  123.     end;
  124.   until not IOerr;
  125. end; { procedure GetDataFromFile }
  126.  
  127. begin { procedure GetData }
  128.   case InputChannel('Input Data From') of
  129.     'K' : GetDataFromKeyboard(Dimen, Data);
  130.     'F' : GetDataFromFile(Dimen, Data);
  131.   end;
  132.   GetOutputFile(OutFile);
  133. end; { procedure GetData }
  134.  
  135. procedure Results(Dimen : integer;
  136.               var Data  : TNmatrix;
  137.                   Det   : Float;
  138.                   Error : byte);
  139.  
  140. {------------------------------------------------------------}
  141. {- This procedure outputs the results to the device OutFile -}
  142. {------------------------------------------------------------}
  143.  
  144. var
  145.   Column, Row : integer;
  146.  
  147. begin
  148.   Writeln(OutFile);
  149.   Writeln(OutFile);
  150.   Writeln(OutFile, 'The matrix: ');
  151.   for Row := 1 to Dimen do
  152.   begin
  153.     for Column := 1 to Dimen do
  154.       Write(OutFile, Data[Row, Column]:18:8);
  155.     Writeln(OutFile);
  156.   end;
  157.   Writeln(OutFile);
  158.   if Error = 1 then
  159.     DisplayError;
  160.  
  161.   case Error of
  162.     0 : Writeln(OutFile, 'Determinant = ', Det);
  163.  
  164.     1 : Writeln(OutFile,
  165.                 'The dimension of the matrix must be greater than 0.');
  166.   end;
  167. end; { procedure Results }
  168.  
  169. begin { Determinant }
  170.   ClrScr;
  171.   Initial(Dimen, Data);
  172.   GetData(Dimen, Data);
  173.   Determinant(Dimen, Data, Det, Error);
  174.   Results(Dimen, Data, Det, Error);
  175.   Close(OutFile);
  176. end. { Determinant }