Subprograms

Subtract procedure

Goal: write a procedure that subtracts two numbers.

Steps:

  1. Complete the procedure Subtract.

Requirements:

  1. Subtract 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

Goal: write a function that subtracts two numbers.

Steps:

  1. Rewrite the Subtract procedure from the previous exercise as a function.

Requirements:

  1. Subtract 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

Goal: write a function that compares two values and returns a flag.

Steps:

  1. Complete the Is_Equal subprogram.

Requirements:

  1. Is_Equal returns a flag as a Boolean value.

  2. The flag must indicate 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;

States

Goal: write a procedure that displays the state of a machine.

Steps:

  1. Complete the procedure Display_State.

Requirements:

  1. The states can be set according to the following numbers:

    Number

    State

    0

    Off

    1

    On: Simple Processing

    2

    On: Advanced Processing

  2. The procedure Display_State receives the number corresponding to a state and displays the state (indicated by the table above) as a user message.

Remarks:

  1. 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:

  1. Implement the function Get_State.

Requirements:

  1. Implement same state machine as in the previous exercise.

  2. Function Get_State must return the state as a string.

Remarks:

  1. 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;
  2. You can reuse your previous implementation and replace it by a case expression.

    1. 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:

  1. Implement the function Is_On.

  2. Implement the procedure Display_On_Off.

Requirements:

  1. Implement same state machine as in the previous exercise.

  2. Function Is_On returns:

    • True if the machine is on;

    • otherwise, it returns False.

  3. Procedure Display_On_Off displays the message

    • "On" if the machine is on, or

    • "Off" otherwise.

  4. Is_On must be called in the implementation of Display_On_Off.

Remarks:

  1. 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:

  1. Implement the procedure Set_Next.

Requirements:

  1. Implement the same state machine as in the previous exercise.

  2. Procedure Set_Next updates the machine's state with the next one in a circular manner:

    • In most cases, the next state of N is simply the next number (N + 1).

    • However, if the state is the last one (which is 2 for our machine), the next state must be the first one (in our case: 0).

Remarks:

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