Labs and Tasks: Functions as Algorithm Wrappers, Assert Statement
Functions and Tuples
Lab, Functions returning tuples:
To do: Create a function that returns the sum and product of two numbers.
✍Algorithm:
function PrintSumMult(x, y: real): (real, real);
begin
Result := (x + y, x * y);
end;
begin
var (a, b) := PrintSumMult(2, 4);
Print(a, b); // 6 8
end.
{0.4 points} Task 1:
To do: Find the minimum and maximum among three numbers. Create a function that returns a tuple containing the minimum and maximum.
Note: To correctly print the results, you must unpack the returned tuple.
Expected output:
enter three numbers:
>>>3 >>>5 >>>1
min = 1, max = 5
---
enter three numbers:
>>>7 >>>0 >>>6
min = 0, max = 7
[Program name: 11task-01.pas]
Lab, Short Definitions + Tuples:
To do: Create a procedure and function to print the sum and product of two numbers. Use short definitions.
✍Algorithm:
Function:
function PrintSumMult(x, y: real) := (x + y, x * y);
begin
var (a, b) := PrintSumMult(2, 4);
Print(a, b); // 6 8
end.
Procedure:
procedure PrintSumMult(x, y: real; var add, mult: real);
begin
add := x + y;
mult := x * y;
end;
begin
var (x, y) := ReadReal2;
var a, b: real;
PrintSumMult(x, y, a, b);
Print(a, b);
end.
{0.5 points} Task 2:
To do: Create a procedure and function inside the same program to print the leftmost and rightmost digits of a two-digit number (the number is entered and passed as a parameter).
Note 1: Must use short definitions.
Note 2: Both procedure and function must be declared above the main program.
Expected output:
please, enter two-digit number: 37
results with function:
3 7
results with procedure:
3 7
[Program name: 11task-02.pas]
Functions as Algorithm Wrappers
{0.3 points} Task 3:
To do: Create a function that outputs the day of the week corresponding to its sequence number (number from 1 to 7).
Expected output:
please, enter a number 1 - 7:
>>>3
Tuesday
---
please, enter a number 1 - 7:
>>>1
Sunday
[Program name: 11task-03.pas]
{0.4 points} Task 4:
To do: Create a function that calculates the number of positive numbers among N randomly generated numbers.
Expected output:
How many numbers?
>>> 5
2 4 -3 5 -4
3 positive numbers
[Program name: 11task-04.pas]
{0.5 points} Task 5:
To do: Create a function that calculates the sum of digits of an entered integer number.
Expected output:
please, enter an integer:
>>>173
sum = 11
[Program name: 11task-05.pas]
{0.4 points} Task 6:
To do: Create a function IsDigit(A)
that returns true
if the entered integer A
is a digit (i.e., falls in the range [0,9]). Inside the main program, invoke this function four times (within a loop) for user-inputted numbers.
Expected output:
Please, enter the number:
>>>35
false
Please, enter the number:
>>>3
true
Please, enter the number:
>>>9
true
Please, enter the number:
>>>10
false
[Program name: 11task-06.pas]
Assert Statement
Task Example
To do: Create a procedure Mean(X, Y, AMean, GMean)
that calculates the arithmetic mean AMean = (X + Y)/2
and the geometric mean GMean = √(XY)
of two positive numbers X
and Y
(X and Y are input parameters, AMean and GMean are output parameters).
✍Решение:
The programmer must secure the program against incorrect input data. The positivity of the parameters X
and Y
is crucial for computing the geometric mean, which happens inside the Mean
procedure. Therefore, the validation must occur inside the Mean
procedure itself:
/// calculates aMean and gMean
procedure Mean(...);
begin
Assert(x > 0);
Assert(y > 0);
// calculating AMean, GMean
// ...
end;
Since the Mean
procedure has two input and two output parameters, each test case must verify that for given X
and Y
values, both output parameters match the expected values. That is:
- Set the values of the input parameters (
X
,Y
). - Call the
Mean
procedure, passing these input values. - Check that the values of the output parameters (
AMean
,GMean
) correspond to the expected ones.
Here is the implementation of the testing procedure:
/// returns true if reals x and y are nearly equal with precision eps
function AreEquals(x, y: real; eps: real): boolean;
begin
Result := Abs(x - y) < eps;
end;
/// Tests the Mean procedure
procedure TestMean(eps: real);
begin
var x, y: real;
var am, gm: real;
// test 1: equal values
x := 2;
y := x;
Mean(x, y, am, gm);
Assert(AreEquals(am, x, eps)
and AreEquals(gm, x, eps));
// test 2: different values
Mean(3, 27, am, gm);
Assert(AreEquals(am, 15, Eps)
and AreEquals(gm, 9, Eps));
// test 3: different values
Mean(6.05, 5, am, gm);
Assert(AreEquals(am, 5.525, Eps)
and AreEquals(gm, 5.5, Eps));
end;
const Eps = 0.00000001;
begin
// call of the test procedure
TestMean(Eps);
// main program
// ...
end.
Lab, assert:
To do: Create a function IsDigit(D)
that returns true
if the entered integer D
is a digit (falls in the range [0,9]). In the main program, output the results of this function for N entered numbers.
✍Algorithm:
function IsDigit(d : integer):= (d >= 0) and (d <= 9);
procedure TestIsDigit;
begin
for var i := 0 to 9 do
Assert(IsDigit(i)=true,'incorrect function algorithm');
end;
begin
TestIsDigit;
var N := ReadInteger();
Assert(n >= 0);
for var i:=1 to n do
begin
var a := ReadInteger();
Print(IsDigit(a));
end;
end.
{0.3 points} Task 7:
To do: Open the solution code of Task 1 (regarding finding minimum and maximum). Add a testing procedure to ensure the correctness of the function. Call this procedure inside the main program.
Note: Add the missing test cases in the indicated place:
procedure TestFindMinMax; // procedure to test the correctness of the function
begin
Assert(findMinMax(4, 1, 5) = (1,5), 'algorithm of findMinMax function is not correct');
Assert(findMinMax(-5, 500, 5) = (-5,500), 'algorithm of findMinMax function is not correct');
// TODO: add 3 or 4 more tests
end;
begin // main program
TestFindMinMax;
//...
end.
Expected output:
enter three numbers:
>>>3 >>>5 >>>1
min = 1, max = 5
---
enter three numbers:
>>>7 >>>0 >>>6
min = 0, max = 7
[Program name: 11task-07.pas]
{0.6 points} Task 8:
To do: Open the solution code of Task 2. Write a procedure to test the correctness of your function. Call this procedure inside the main program.
Note: Follow the example from the Lab (Assert section) to perform tests: 1) checking if the entered number is indeed a two-digit number; 2) ensuring the test procedure provides accurate results (perform 2–3 tests).
function IsTwoDigit(...):= ...;
procedure Test...; // procedure to test the correctness of the function
begin
Assert(..., 'error: the function is incorrect'); // test #1
Assert(..., 'error: the function is incorrect'); // test #2
end;
begin // main program
Test...;
var numb:=ReadInteger('please, enter two-digit number:');
Assert(IsTwoDigit(numb), 'error: number must be of two digits');
//...
end.
Expected output:
please, enter two-digit number: 37
results with function:
3 7
[Program name: 11task-08.pas]
{0.5 points} Task 9:
To do: Open the solution code of Task 3. Create a procedure to test the correctness of your function (provide 3–4 tests). Call this procedure inside the main program. Additionally, make sure to validate the input (ensure the quantity of generated numbers is greater than zero).
Expected output:
please, enter a number 1 - 7:
>>>3
Tuesday
---
please, enter a number 1 - 7:
>>>1
Sunday
---
please, enter a number 1 - 7:
>>>0
! exception: entered number must be in the interval 1 - 7
[Program name: 11task-09.pas]
{0.5 points} Task 10:
To do: Open the solution code of Task 5. Create a procedure to test the correctness of your function (provide 3–4 tests). Call this procedure inside the main program.
Expected output:
please, enter an integer:
>>>173
sum = 11
[Program name: 11task-10.pas]