Solution of the simple task: Begin3
This section contains description of solving the following simple task in Borland Delphi:
Begin3°. The sides a and b of a rectangle are given.
Find the area S = ab and the perimeter P = 2(a +
b) of the rectangle.
Creating a template and acquaintance with the task
To create a template of the required task one should use PT4Load tool.
The template created for Delphi environment
consists of Begin3.dpr file that contains
the following text:
{$D+,L+,I+,Q+,R+,S+}
program Begin3;
uses PT4;
begin
Task('Begin3');
end.
The first statement is compiler directives (among them there are
input-output checking {I+}, overflow checking {Q+}, and range checking {R+}). The
statement with uses clause add the PT4 module to the program.
The last statement is the
Task procedure call that initializes the Begin3 task.
To run the program press [F9] key.
When the program is launched
you will see the Programming Taskbook window with
a task text and initial data.
This running is considered as acquaintance running because
the program does not perform input-output operations.
To close the Programming Taskbook window click the "Exit" button or press [Esc] or {F9] key.
Initial data input
Before solving tasks you should input initial data in the program.
In the Begin3 task the initial data are real numbers a and b
that refer to sides of the rectangle.
The initial data should be input in the variable of the required type, otherwise Programming Taskbook
will detect error. Let's model this situation in our program. For this purpose we shall organize initial data
input to variables of the integer type using the GetN procedure
(the line of compile directives and the PROGRAM statement will not be show for brevity):
uses PT4;
var
a, b: integer;
begin
Task('Begin3');
GetN(a);
GetN(b);
end.
Run the program once again. You will see the new set of initial data. For each running of the
program a new initial data are generated, so it is necessary to develop an algorithm that processes correctly
all admissible sets of initial data.
In this case the running leads to the following information in the status bar:
"Invalid type is used for an input data item."
To input data correctly we should use the GetR procedure that
provides input data of real type. Change our program as follows:
uses PT4;
var
a, b: real;
begin
Task('Begin3');
GetR(a);
GetR(b);
end.
Now data input is performed correctly, but the program does not output results.
Therefore we have the following message: "Some data are not output.".
Calculation and output results
Let's perform the required calculations and output results using the PutR
procedure:
uses PT4;
var
a, b, S, P: real;
begin
Task('Begin3');
GetR(a);
GetR(b);
S := a * b;
P := 2 * (a + b);
PutR(P);
PutR(S);
end.
When the program is running you can see output values at the panel of results.
Because we output data in inverse order, the status bar contains the error message "Wrong solution".
Right solution and its testing
To correct the last error it is enough to change order of two output statements:
PutR(S);
PutR(P);
When this program is running you will see the following information in the status bar: "Right solution.
The test 1 of 3".
We have performed successfully the first test of our program.
But the correctness of the algorithm must be proved by a number of tests with various initial data.
The amount of tests is in range from 3 to 9 and depends on the level of the task difficulty
(for the Begin3 task it is enough 3 tests).
If all required tests are performed successfully then the message
"The task is solved!" will be shown at the status bar.
If some test is failed then the algorithm should be corrected and tested again.
In our case the algorithm is correct, therefore the message
"The task is solved!" will be shown after 3 test runnings.
To browse information about the task solving you can use PT4Results tool.
uses PT4;
var
a, b: real;
begin
Task('Begin3');
GetR(a);
GetR(b);
PutR(a * b);
PutR(2 * (a + b));
end.
|