Subprograms
Subtract procedure
Goal: write a procedure that subtracts two numbers.
Steps:
Complete the procedure
Subtract.
Requirements:
Subtractperforms the operationA - B.
-- Write the correct parameters for the procedure below.
procedure Subtract;
procedure Subtract is
begin
-- Implement the procedure here.
null;
end Subtract;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Subtract;
procedure Main is
type Test_Case_Index is
(Sub_10_1_Chk,
Sub_10_100_Chk,
Sub_0_5_Chk,
Sub_0_Minus_5_Chk);
procedure Check (TC : Test_Case_Index) is
Result : Integer;
begin
case TC is
when Sub_10_1_Chk =>
Subtract (10, 1, Result);
Put_Line ("Result: " & Integer'Image (Result));
when Sub_10_100_Chk =>
Subtract (10, 100, Result);
Put_Line ("Result: " & Integer'Image (Result));
when Sub_0_5_Chk =>
Subtract (0, 5, Result);
Put_Line ("Result: " & Integer'Image (Result));
when Sub_0_Minus_5_Chk =>
Subtract (0, -5, Result);
Put_Line ("Result: " & Integer'Image (Result));
end case;
end Check;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
Check (Test_Case_Index'Value (Argument (1)));
end Main;
Subtract function
Goal: write a function that subtracts two numbers.
Steps:
Rewrite the
Subtractprocedure from the previous exercise as a function.
Requirements:
Subtractperforms the operationA - Band returns the result.
-- Write the correct signature for the function below.
-- Don't forget to replace the keyword "procedure" by "function."
procedure Subtract;
procedure Subtract is
begin
-- Implement the function here!
null;
end Subtract;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Subtract;
procedure Main is
type Test_Case_Index is
(Sub_10_1_Chk,
Sub_10_100_Chk,
Sub_0_5_Chk,
Sub_0_Minus_5_Chk);
procedure Check (TC : Test_Case_Index) is
Result : Integer;
begin
case TC is
when Sub_10_1_Chk =>
Result := Subtract (10, 1);
Put_Line ("Result: " & Integer'Image (Result));
when Sub_10_100_Chk =>
Result := Subtract (10, 100);
Put_Line ("Result: " & Integer'Image (Result));
when Sub_0_5_Chk =>
Result := Subtract (0, 5);
Put_Line ("Result: " & Integer'Image (Result));
when Sub_0_Minus_5_Chk =>
Result := Subtract (0, -5);
Put_Line ("Result: " & Integer'Image (Result));
end case;
end Check;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
Check (Test_Case_Index'Value (Argument (1)));
end Main;
Equality function
Goal: write a function that compares two values and returns a flag.
Steps:
Complete the
Is_Equalsubprogram.
Requirements:
Is_Equalreturns a flag as aBooleanvalue.The flag must indicate whether the values are equal (flag is
True) or not (flag isFalse).
-- Write the correct signature for the function below.
-- Don't forget to replace the keyword "procedure" by "function."
procedure Is_Equal;
procedure Is_Equal is
begin
-- Implement the function here!
null;
end Is_Equal;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Is_Equal;
procedure Main is
type Test_Case_Index is
(Equal_Chk,
Inequal_Chk);
procedure Check (TC : Test_Case_Index) is
procedure Display_Equal (A, B : Integer;
Equal : Boolean) is
begin
Put (Integer'Image (A));
if Equal then
Put (" is equal to ");
else
Put (" isn't equal to ");
end if;
Put_Line (Integer'Image (B) & ".");
end Display_Equal;
Result : Boolean;
begin
case TC is
when Equal_Chk =>
for I in 0 .. 10 loop
Result := Is_Equal (I, I);
Display_Equal (I, I, Result);
end loop;
when Inequal_Chk =>
for I in 0 .. 10 loop
Result := Is_Equal (I, I - 1);
Display_Equal (I, I - 1, Result);
end loop;
end case;
end Check;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
Check (Test_Case_Index'Value (Argument (1)));
end Main;
States
Goal: write a procedure that displays the state of a machine.
Steps:
Complete the procedure
Display_State.
Requirements:
The states can be set according to the following numbers:
Number
State
0
Off
1
On: Simple Processing
2
On: Advanced Processing
The procedure
Display_Statereceives the number corresponding to a state and displays the state (indicated by the table above) as a user message.
Remarks:
You can use a case statement to implement this procedure.
procedure Display_State (State : Integer);
with Ada.Text_IO; use Ada.Text_IO;
procedure Display_State (State : Integer) is
begin
null;
end Display_State;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Display_State;
procedure Main is
State : Integer;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
State := Integer'Value (Argument (1));
Display_State (State);
end Main;
States #2
Goal: write a function that returns the state of a machine.
Steps:
Implement the function
Get_State.
Requirements:
Implement same state machine as in the previous exercise.
Function
Get_Statemust return the state as a string.
Remarks:
You can implement a function returning a string by simply using quotes in a return statement. For example:
function Get_Hello return String;function Get_Hello return String is begin return "Hello"; end Get_Hello;with Ada.Text_IO; use Ada.Text_IO; with Get_Hello; procedure Main is S : constant String := Get_Hello; begin Put_Line (S); end Main;You can reuse your previous implementation and replace it by a case expression.
For values that do not correspond to a state, you can simply return an empty string (
"").
function Get_State (State : Integer) return String;
function Get_State (State : Integer) return String is
begin
return "";
end Get_State;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Get_State;
procedure Main is
State : Integer;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
State := Integer'Value (Argument (1));
Put_Line (Get_State (State));
end Main;
States #3
Goal: implement an on/off indicator for a state machine.
Steps:
Implement the function
Is_On.Implement the procedure
Display_On_Off.
Requirements:
Implement same state machine as in the previous exercise.
Function
Is_Onreturns:
Trueif the machine is on;otherwise, it returns
False.Procedure
Display_On_Offdisplays the message
"On" if the machine is on, or
"Off" otherwise.
Is_Onmust be called in the implementation ofDisplay_On_Off.
Remarks:
You can implement both subprograms using if expressions.
function Is_On (State : Integer) return Boolean;
function Is_On (State : Integer) return Boolean is
begin
return False;
end Is_On;
procedure Display_On_Off (State : Integer);
with Ada.Text_IO; use Ada.Text_IO;
with Is_On;
procedure Display_On_Off (State : Integer) is
begin
Put_Line ("");
end Display_On_Off;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Display_On_Off;
with Is_On;
procedure Main is
State : Integer;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
State := Integer'Value (Argument (1));
Display_On_Off (State);
Put_Line (Boolean'Image (Is_On (State)));
end Main;
States #4
Goal: implement a procedure to update the state of a machine.
Steps:
Implement the procedure
Set_Next.
Requirements:
Implement the same state machine as in the previous exercise.
Procedure
Set_Nextupdates the machine's state with the next one in a circular manner:
In general, after a call to
Set_Nextfor an integer variableN(Set_Next (N)), the new value ofNmust be the next number for that variableN(i.e.,N := N'Old + 1).
In other words, a call to
Set_Next (N)has the same effect asN := N + 1.For example, after the statements
N := 1; Set_Next (N);, we have thatN = 2.However, if the state is the last valid one for the machine (which, for this exercise, it's 2), the next state must be the first valid one (in this case: 0).
In other words, for
N := 2; Set_Next (N);, we have thatN = 0.
Remarks:
You can use an if expression to implement
Set_Next.
procedure Set_Next (State : in out Integer);
procedure Set_Next (State : in out Integer) is
begin
null;
end Set_Next;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Set_Next;
procedure Main is
State : Integer;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
State := Integer'Value (Argument (1));
Set_Next (State);
Put_Line (Integer'Image (State));
end Main;