Reputation: 21
I am new in Ada.
My problem is:
Create in programming language ADA:
There are 6 lakes. Each of lakes has:
4 swans with number of feathers: 100, 200, 300, 400 and
7 geese with number of feathers: 500, 600, 700, 800, 900, 1000, 1100.
There are 10 hunters. Each hunter plans to hunt 3 swans and 5 geese. Each hunter hunts in as many different lakes as possible (second bird from the same lake hunter hunts only after he has tried successfully or unsuccessfully to hunt in other lakes). Each time a hunter chooses to hunt a bird with most feathers from the available birds of the type he is looking for.
2 hunters first look for swans (and search for geese only after they have hunt required number of swans or when no more swans is in lakes), and 8 hunters first look for geese. When hunter has obtained required number of swans and geese, he says the total number of feathers he has hunted. The hunter, who has not obtained the required number of feathers, says how many swans and how many geese he is missing.
Instructions: Describe both hunters and lakes as TASK arrays in ADA; if hunter x wants to hunt a swan from lake y, hunter calls the lake from which either he receives the number of feathers received during synchronization or he receives rejection if there is no any swan or geese in lake. The disclosure of the output data must be made by printing them at the standard output.
All hunters must complete their job in parallel. All TASKs in the program and the program itself must quit work. It is forbidden to use time control to ensure that the program stops working.
Information on the activities of the hunters, the birds that they hunt, and remaining of swans or geese in each lake it is forbidden to keep in global variables or in any other structure outside the hunts or lakes. Information is exchanged between the main application and the taskbooks only on the basis of message exchange; tasks does not use the access to variables defined outside them.
First I want to understand how to define the rules. My solution at this time is this:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Birds is
type Hunter (Swans, Geese);
type Swans(100,200,300,400) of Integer;
type Geese(500,600,700,800,900,1000,1100) of Integer;
Hunter_Number_Of_Birds : INTEGER := 0;
Lake_Number_Of_birds_In_Lake : INTEGER := 66;
-- The main program adds and deletes birds to hunters and from the lakes.
task Hunter is
entry Hunts_Swans(dates: in Integer); -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer); -- This adds geese to hunter
end Hunter;
task Lake is
entry Swans(dates: in Integer); -- This deletes swans from lake
entry Geese(dates: in Integer); -- This deletes geese from lake
end Lake;
task body Hunter is
begin
for Index in 1..8 loop
select
when Hunter_Number_Of_Birds =>8
accept Hunts_Swans do
Hunter_Number_Of_Birds := Hunter_Number_Of_Birds + 1;
Put_line("Hunter adds swans, count =");
Put(Hunter_Number_Of_Birds);
New_Line;
end Hunts_Swans;
or
when Hunter_Number_Of_Birds =>8
accept Hunts_Geese do
Hunter_Number_Of_Birds := Hunter_Number_Of_Birds + 1;
Put_Line("Hunter adds geese, count =");
Put(Hunter_Number_Of_Birds);
end Hunts_Geese;
end select;
end loop;
end Hunter;
task body Lake is
begin
for Index in 1..5 loop
select
when Lake_Number_Of_birds_In_Lake =>66
accept Swans do
Lake_Number_Of_birds_In_Lake := Lake_Number_Of_birds_In_Lake - 1;
Put_line("Hunter hunt swans, remaining number of swans in the lake =");
Put(Lake_Number_Of_birds_In_Lake);
New_Line;
end Swans;
or
when Lake_Number_Of_birds_In_Lake =>66
accept Geese do
Lake_Number_Of_birds_In_Lake:= Lake_Number_Of_birds_In_Lake - 1;
Put_line("Hunter hunt geese, remaining number of geese in the lake =");
Put(Lake_Number_Of_birds_In_Lake);
end Geese;
end select;
end loop;
end Lake;
I have no idea how can I define:
Upvotes: 0
Views: 1179
Reputation: 3641
Number of hunters and lakes;
As mentioned in the comments, change your task declarations to task types:
task type Hunter is
entry Hunts_Swans(dates: in Integer); -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer); -- This adds geese to hunter
end Hunter;
task type Lake is
entry Swans(dates: in Integer); -- This deletes swans from lake
entry Geese(dates: in Integer); -- This deletes geese from lake
end Lake;
Then you just need to declare them as arrays:
type Hunter_Array is array(1..10) of Hunter;
type Lake_Array is array(1..6) of Lake;
Hunters : Hunter_Array;
Lakes : Lake_Array;
number of feathers of each beards;
Here I would create a birds package and use Ada.Containers.Bounded_Vectors:
package Birds is
type Bird is (Swan,Goose);
type Feather_Count is new Natural;
end Birds;
use type Birds.Feather_Count;
package Vectors is new Ada.Containers.Bounded_Vectors
(Index_Type => Positive,
Element_Type => Birds.Feather_Count);
use type Vectors.Vector;
Swans : aliased Vectors.Vector
:= Vectors.To_Vector(100,1) & 200 & 300 & 400;
Geese : aliased Vectors.Vector
:= Vectors.To_Vector(500,1) & 600 & 700 & 800 & 900 & 1000 & 1100;
Using the vector allows you to keep track of the number of birds left in the lake (you can call Swans.Length for example) and when hunted you can get the feathers by calling
Feathers := Swans.Last_Element;
Swans.Delete_Last; -- Takes it out of the vector
Essentially using the vector as a stack.
2 hunters first look for swans and 8 hunters first look for geese;
For this I would change your Hunter task definition to:
task type Hunter(Prefers : Birds.Bird := Birds.Swan) is
entry Hunts_Swans(dates: in Integer); -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer); -- This adds geese to hunter
end Hunter;
and add a constructing function for it:
function Swan_Hunter return Hunter is
begin
return Result : Hunter(Birds.Swan);
end Swan_Hunter;
function Goose_Hunter return Hunter is
begin
return Result : Hunter(Birds.Goose);
end Goose_Hunter;
Then you can change your Hunter array declaration to:
Hunters : Hunter_Array :=
(1 => Swan_Hunter,
2 => Swan_Hunter,
others => Goose_Hunter);
After that you just need to add the logic to keep track of if they are currently hunting geese or swan and compare it to their preference to know how to proceed.
count the feathers
When you hunt from a lake, have the entry also return the number of feathers. Then you can just add them together everytime it is successful and have the hunter keep a running total for himself/herself.
Upvotes: 1