Subprograms

Subtract procedure

In this exercise, you'll write a procedure called Subtract. As the name indicates, this procedure performs the operation A - 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

In this exercise, you'll rewrite the Subtract procedure as a function. As in the previous exercise, this function performs the operation A - B and 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

In this exercise, you'll write a function that compares two values and returns a flag (Boolean value) indicating whether the values are equal (flag is True) or not (flag is False).

-- 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;