Solution of the simple task: Begin3
This section contains description of solving the following simple task in PascalABC.NET:
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 you should use the PT4Load tool.
Just run PT4Load.exe with the menu command "Plugins|Create Program Template"
(you can also use the button on the toolbar of the PascalABC.NET window or the combination Ctrl+Shift+L).
The template created for PascalABC.NET environment
consists of Begin3.pas file that contains the following text:
uses PT4;
begin
Task('Begin3');
end.
The statement with uses clause adds the PT4 module to the program.
The last statement is the
Task procedure call that initializes the Begin3 task.
Note that when performing tasks in the PascalABC.NET environment, you should ensure that the successfully
compiled file will be saved automatically. To do this, run the menu command "Tools | Options..."
and in the window that appears in the "General" section check the box "Save files if compilation is successful".
Besides, it is desirable not to use the mode of automatic deletion of an exe-file after completion of the program.
For this purpose, you should uncheck the "Delete EXE after run" option in the "Compiler Options" section
of the same options window. After setting the required options, close the "Options" dialog box by clicking the "OK" button.
To run the program, press [F9].
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 ReadInteger2 function
(the uses statement will not be shown for brevity):
begin
Task('Begin3');
var (a, b) := ReadInteger2;
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." Note that each type of errors is marked by different color.
To input data correctly, we should use the ReadReal2 function that
provides input data of real type. Change our program as follows:
begin
Task('Begin3');
var (a, b) := ReadReal2;
end.
Now data input is performed correctly, but the program does not output results.
In such situation we have
the following message: "Correct data input: all required data are input, no data are output".
Calculation and output results
Let's perform the required calculations and output results using the Print
procedure:
begin
Task('Begin3');
var (a, b) := ReadReal2;
var (P, S) := (2 * (a + b), a * b);
Print(P, 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
Let's correct the mistake by swapping the parameters in the Print procedure:
Print(S, P);
When this program is running you will see the testing panel on screen:
This panel appears when the program processes successfully at least one set of input data.
The progress bar shows the amount of tests which are already performed,
the text above the bar allows to determine how much tests should be performed successfully.
The program testing finishes in two cases: when all required tests are performed successfully
or when some test is failed. If the program is in infinite loop, then
you must terminate the program by executing the menu command "Program | Stop"
(you can also press the button with a blue square on the toolbar or the key [Ctrl]+[F2]).
In our case, the algorithm is correct, therefore the message
"The task is solved!" will be shown.
To browse information about the task solving, you can use PT4Results tool
(you may run this tool by pressing the [F2] key or clicking the Results label in the upper-right corner of
the Programming Taskbook window):
Begin3 p23/02 20:13 Acquaintance with the task.
Begin3 A04/02 15:07 Invalid type is used for an input data item.
Begin3 A04/02 15:09 Correct data input.
Begin3 A04/02 15:12 Wrong solution.
Begin3 A04/02 15:15 The task is solved!
The letter "A" denotes the programming language being used (PascalABC.NET).
begin
Task('Begin3');
var (a, b) := ReadReal2;
Print(a * b, 2 * (a + b));
end.
|