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

  1.                                        -- Chapter 20 - Program 3
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure Discrim3 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 := 2) 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.    Var_Store   : STUFF;
  26.  
  27.    function Add_Elements(In_Array : STUFF) return INTEGER is
  28.    Total : INTEGER := 0;
  29.    begin
  30.       for Index1 in In_Array.Matrix'RANGE(1) loop
  31.          for Index2 in In_Array.Matrix'RANGE(2) loop
  32.             Total := Total + In_Array.Matrix(Index1,Index2);
  33.          end loop;
  34.       end loop;
  35.       return Total;
  36.    end Add_Elements;
  37.  
  38.    procedure Set_To_Ones(Work_Array : in out STUFF) is
  39.    begin
  40.       for Index1 in Work_Array.Matrix'RANGE(1) loop
  41.          for Index2 in Work_Array.Matrix'RANGE(2) loop
  42.             Work_Array.Matrix(Index1,Index2) := 1;
  43.          end loop;
  44.       end loop;
  45.    end Set_To_Ones;
  46.  
  47. begin
  48.  
  49.    for Index1 in 1..Data_Store.List_Size loop
  50.       Data_Store.Linear(Index1) := Index1;
  51.       for Index2 in 1..Data_Store.List_Size loop
  52.          Data_Store.Matrix(Index1,Index2) := Index1 * Index2;
  53.       end loop;
  54.    end loop;
  55.  
  56.    Var_Store := Data_Store;
  57.    Put("The total of Var_Store is ");
  58.    Put(Add_Elements(Var_Store));
  59.    New_Line;
  60.  
  61.    Set_To_Ones(Var_Store);
  62.    Put("The total of Var_Store is ");
  63.    Put(Add_Elements(Var_Store));
  64.    New_Line;
  65.  
  66.    Set_To_Ones(Big_Store);
  67.    Var_Store := Big_Store;
  68.    Put("The total of Var_Store is ");
  69.    Put(Add_Elements(Var_Store));
  70.    New_Line;
  71.  
  72. end Discrim3;
  73.  
  74.  
  75.  
  76.  
  77. -- Result of execution
  78.  
  79. -- The total of Var_Store is    225
  80. -- The total of Var_Store is     25
  81. -- The total of Var_Store is    144
  82.  
  83.