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

  1.                                     -- Chapter 27 - Program 4
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure Parallel is
  6.  
  7. package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8. use Int_IO;
  9.  
  10. DAYS : constant := 7;
  11. EMPLOYEES : constant := 11;
  12.  
  13. type WORKED_ARRAY is array (1..EMPLOYEES,1..DAYS) of INTEGER;
  14. type TOTAL_ARRAY is array (1..EMPLOYEES) of INTEGER;
  15.  
  16. Hours_Worked  : WORKED_ARRAY;
  17. Weekly_Totals : TOTAL_ARRAY;
  18. Result        : INTEGER;
  19.  
  20. task type SUMMING_TASK_TYPE is
  21.    entry Start_Sums(E_No : in INTEGER);
  22.    entry Return_Sum(Result : out INTEGER);
  23. end SUMMING_TASK_TYPE;
  24.  
  25. Adding_Task : array(1..EMPLOYEES) of SUMMING_TASK_TYPE;
  26. Emp_Number : INTEGER;
  27.  
  28. task body SUMMING_TASK_TYPE is
  29. Total : INTEGER := 0;
  30. begin
  31.    accept Start_Sums(E_No : in INTEGER) do
  32.       Emp_Number := E_No;
  33.    end Start_Sums;
  34.  
  35.    for Index in 1..Days loop
  36.       Total := Total + Hours_Worked(Emp_Number,Index);
  37.    end loop;
  38.  
  39.    accept Return_Sum(Result : out INTEGER) do
  40.       Result := Total;
  41.    end Return_Sum;
  42. end SUMMING_TASK_TYPE;
  43.  
  44. begin
  45.    for Emp_Number in 1..EMPLOYEES loop
  46.       for Day in 1..DAYS loop
  47.          Hours_Worked(Emp_Number,Day) := 8;
  48.       end loop;
  49.    end loop;
  50.    Hours_Worked(2,5) := 3;
  51.    Hours_Worked(3,5) := 0;
  52.  
  53.    Put_Line("The Hours_Worked array is filled");
  54.  
  55.                                 -- Start all parallel additions
  56.    for Emp_Number in 1..EMPLOYEES loop
  57.       Adding_Task(Emp_Number).Start_Sums(Emp_Number);
  58.    end loop;
  59.  
  60.                                 -- Get the results back
  61.    for Emp_Number in 1..EMPLOYEES loop
  62.       Adding_Task(Emp_Number).Return_Sum(Result);
  63.       Weekly_Totals(Emp_Number) := Result;
  64.    end loop;
  65.  
  66.    for Emp_Number in 1..EMPLOYEES loop
  67.       Put("Employee number");
  68.       Put(Emp_Number,3);
  69.       Put(" worked");
  70.       Put(Weekly_Totals(Emp_Number),3);
  71.       Put_Line(" hours.");
  72.    end loop;
  73.  
  74. end Parallel;
  75.  
  76.  
  77.  
  78.  
  79. -- Result of execution
  80.  
  81. -- The Hours_Worked array is filled
  82. -- Employee number  1 worked 56 hours
  83. -- Employee number  2 worked 51 hours
  84. -- Employee number  3 worked 48 hours
  85. -- Employee number  4 worked 56 hours
  86. -- Employee number  5 worked 56 hours
  87. -- Employee number  6 worked 56 hours
  88. -- Employee number  7 worked 56 hours
  89. -- Employee number  8 worked 56 hours
  90. -- Employee number  9 worked 56 hours
  91. -- Employee number 10 worked 56 hours
  92. -- Employee number 11 worked 56 hours
  93.  
  94.