home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / objgen.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.3 KB  |  92 lines

  1.                                    -- Chapter 29 - Program 3
  2. generic
  3.    type ITEM is range <>;
  4.    ROWS    : in POSITIVE := 8;
  5.    COLUMNS : in POSITIVE := 12;
  6. package Matrix_Operations is
  7.  
  8.    type LOCAL_MATRIX is private;
  9.  
  10.    procedure Clear_Matrix(Matrix_To_Clear : in out LOCAL_MATRIX);
  11.  
  12.    function Add_Matrices(M1, M2 : LOCAL_MATRIX) return LOCAL_MATRIX;
  13.  
  14. private
  15.    type LOCAL_MATRIX is array(POSITIVE range 1..ROWS,
  16.                               POSITIVE range 1..COLUMNS) of ITEM;
  17. end Matrix_Operations;
  18.  
  19.  
  20.  
  21.  
  22. with Text_IO;
  23. use Text_IO;
  24. package body Matrix_Operations is
  25.  
  26.    procedure Clear_Matrix(Matrix_To_Clear : in out LOCAL_MATRIX) is
  27.    begin
  28.       for Row in 1..Matrix_To_Clear'LAST(1) loop
  29.          for column in 1.. Matrix_To_Clear'LAST(2) loop
  30.             Matrix_To_Clear(Row,Column) := 0;
  31.          end loop;
  32.       end loop;
  33.       Put_Line("  A matrix has been cleared");
  34.    end Clear_Matrix;
  35.  
  36.    function Add_Matrices(M1, M2 : LOCAL_MATRIX) return LOCAL_MATRIX is
  37.    Temp : LOCAL_MATRIX;
  38.    begin
  39.       for Row in 1..M1'LAST(1) loop
  40.          for column in 1.. M1'LAST(2) loop
  41.             Temp(Row, Column) := M1(Row, Column) + M2(Row, Column);
  42.          end loop;
  43.       end loop;
  44.       Put_Line("  Two matrices have been summed");
  45.       return Temp;
  46.    end Add_Matrices;
  47.  
  48. end Matrix_Operations;
  49.  
  50.  
  51.  
  52.  
  53. with Text_IO, Matrix_Operations;
  54. use Text_IO;
  55.  
  56. procedure ObjGen is
  57.  
  58. package Cookie_Jar is new Matrix_Operations(NATURAL,3,5);
  59. use Cookie_Jar;
  60. package Puppies is new Matrix_Operations(INTEGER);
  61. package Animals is new Matrix_Operations(COLUMNS => 7,
  62.                                          ROWS => 11,
  63.                                          ITEM => INTEGER);
  64. use Animals;
  65.  
  66. Cookie_Matrix                : Cookie_Jar.LOCAL_MATRIX;
  67. Dog_Matrix                   : Puppies.LOCAL_MATRIX;
  68. Cattle, Jerseys, Black_Angus : Animals.LOCAL_MATRIX;
  69.  
  70. begin
  71.    Put_Line("Begin the matrix operations.");
  72.    Clear_Matrix(Cookie_Matrix);
  73.    Puppies.Clear_Matrix(Dog_Matrix);
  74.    Clear_Matrix(Jerseys);
  75.    Clear_Matrix(Black_Angus);
  76.    Cattle := Add_Matrices(Jerseys, Black_Angus);
  77.  
  78. end ObjGen;
  79.  
  80.  
  81.  
  82.  
  83. -- Result of execution
  84.  
  85. -- Begin the matrix operations
  86. --   A matrix has been cleared
  87. --   A matrix has been cleared
  88. --   A matrix has been cleared
  89. --   A matrix has been cleared
  90. --   Two matrices have been summed
  91.  
  92.