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

  1.                                        -- Chapter 20 - Program 2
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure Discrim2 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    type SQUARE is array(INTEGER range <>,
  11.                         INTEGER range <>) of INTEGER;
  12.  
  13.    type LINEAR_TYPE is array(INTEGER range <>) of POSITIVE;
  14.  
  15.    type STUFF(List_Size : POSITIVE) is
  16.       record
  17.          Matrix   : SQUARE(1..List_Size,1..List_Size);
  18.          Elements : INTEGER := List_Size * List_Size;
  19.          Linear   : LINEAR_TYPE(1..List_Size);
  20.          Number   : INTEGER := List_Size;
  21.       end record;
  22.  
  23.    Data_Store  : STUFF(5);
  24.    Big_Store   : STUFF(12);
  25.  
  26.    function Add_Elements(In_Array : STUFF) return INTEGER is
  27.    Total : INTEGER := 0;
  28.    begin
  29.       for Index1 in In_Array.Matrix'RANGE(1) loop
  30.          for Index2 in In_Array.Matrix'RANGE(2) loop
  31.             Total := Total + In_Array.Matrix(Index1,Index2);
  32.          end loop;
  33.       end loop;
  34.       return Total;
  35.    end Add_Elements;
  36.  
  37.    procedure Set_To_Ones(Work_Array : in out STUFF) is
  38.    begin
  39.       for Index1 in Work_Array.Matrix'RANGE(1) loop
  40.          for Index2 in Work_Array.Matrix'RANGE(2) loop
  41.             Work_Array.Matrix(Index1,Index2) := 1;
  42.          end loop;
  43.       end loop;
  44.    end Set_To_Ones;
  45.  
  46. begin
  47.  
  48.    for Index1 in 1..Data_Store.List_Size loop
  49.       Data_Store.Linear(Index1) := Index1;
  50.       for Index2 in 1..Data_Store.List_Size loop
  51.          Data_Store.Matrix(Index1,Index2) := Index1 * Index2;
  52.       end loop;
  53.    end loop;
  54.  
  55.    Set_To_Ones(Big_Store);
  56.  
  57.    Put("The total of Data_Store is");
  58.    Put(Add_Elements(Data_Store));
  59.    New_Line;
  60.  
  61.    Put("The total of Big_Store is ");
  62.    Put(Add_Elements(Big_Store));
  63.    New_Line;
  64.  
  65. end Discrim2;
  66.  
  67.  
  68.  
  69.  
  70. -- Result of execution
  71.  
  72. -- The total of Data_Store is   225
  73. -- The total of Big_Store is    144
  74.  
  75.