-- Chapter 29 - Program 4
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Parallel is
DAYS : constant := 7;
EMPLOYEES : constant := 11;
type WORKED_ARRAY is array (1..EMPLOYEES, 1..DAYS) of INTEGER;
type TOTAL_ARRAY is array (1..EMPLOYEES) of INTEGER;
Hours_Worked : WORKED_ARRAY;
Weekly_Totals : TOTAL_ARRAY;
Result : INTEGER;
task type SUMMING_TASK_TYPE is
entry Start_Sums(E_No : in INTEGER);
entry Return_Sum(Result : out INTEGER);
end SUMMING_TASK_TYPE;
Adding_Task : array(1..EMPLOYEES) of SUMMING_TASK_TYPE;
task body SUMMING_TASK_TYPE is
Total : INTEGER := 0;
Local_E_No : INTEGER;
begin
accept Start_Sums(E_No : in INTEGER) do
Local_E_No := E_No;
end Start_Sums;
for Index in 1..Days loop
Total := Total + Hours_Worked(Local_E_No, Index);
end loop;
accept Return_Sum(Result : out INTEGER) do
Result := Total;
end Return_Sum;
end SUMMING_TASK_TYPE;
begin
for Emp_Number in 1..EMPLOYEES loop
for Day in 1..DAYS loop
Hours_Worked(Emp_Number, Day) := 8;
end loop;
end loop;
Hours_Worked(2, 5) := 3;
Hours_Worked(3, 5) := 0;
Put_Line("The Hours_Worked array is filled");
-- Start all parallel additions
for Emp_Number in 1..EMPLOYEES loop
Adding_Task(Emp_Number).Start_Sums(Emp_Number);
end loop;
-- Get the results back
for Emp_Number in 1..EMPLOYEES loop
Adding_Task(Emp_Number).Return_Sum(Result);
Weekly_Totals(Emp_Number) := Result;
end loop;
for Emp_Number in 1..EMPLOYEES loop
Put("Employee number");
Put(Emp_Number, 3);
Put(" worked");
Put(Weekly_Totals(Emp_Number), 3);
Put_Line(" hours.");
end loop;
end Parallel;
-- Result of execution
-- The Hours_Worked array is filled
-- Employee number 1 worked 56 hours
-- Employee number 2 worked 51 hours
-- Employee number 3 worked 48 hours
-- Employee number 4 worked 56 hours
-- Employee number 5 worked 56 hours
-- Employee number 6 worked 56 hours
-- Employee number 7 worked 56 hours
-- Employee number 8 worked 56 hours
-- Employee number 9 worked 56 hours
-- Employee number 10 worked 56 hours
-- Employee number 11 worked 56 hours