Programming Taskbook

Russian

E-mail:

Password:

User registration   Restore password

1000 training tasks on programming

©  M. E. Abramyan, 1998–2010

 

Examples | Visual Basic | Simple task

PrevNext


Solution of the simple task: Begin3

This section contains description of solving the following simple task in Microsoft Visual Basic:

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.

In this and the following sections we shall use the "procedure" notion for Visual Basic subroutines.

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 Visual Basic 5.0 and 6.0 consists of several files. But we need only Begin.bas file. This file will be loaded into the editor of Visual Basic:

Option Explicit
Public Sub Solve()
Task "Begin3"

End Sub

The file contains the description of the Solve procedure, which the solution of the task should be entered in (of course any other procedures and functions may be used). The Solve procedure already contains the Task procedure call that initializes the Begin3 task.

Note that the Main procedure is not defined in the Begin3.bas file. This procedure is defined in the pt4.bas file that was created when the project was generated for this task. The pt4.pas file is not loaded into the editor of Visual Basic. But the project includes this file (see the Project window located on the Visual Basic window).

To run the program press [F5] 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 [F5] 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:

Public Sub Solve()
Task "Begin3"
Dim a As Integer, b As Integer
GetN a
GetN b
End Sub

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 GetD procedure that provides input data of Double type. Change our program as follows:

Public Sub Solve()
Task "Begin3"
Dim a As Double, b As Double
GetD a
GetD b
End Sub

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 PutD procedure:

Public Sub Solve()
Task "Begin3"
Dim a As Double, b As Double
GetD a
GetD b
Dim S As Double, P As Double
S = a * b
P = 2 * (a + b)
PutD P
PutD S
End Sub

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 in the Solve procedure:

PutD S
PutD 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.

Remark. It is not necessary to use additional variables S and P, because you can output expressions:

Public Sub Solve()
Task "Begin3"
Dim a As Double, b As Double
GetD a
GetD b
PutD a * b
PutD 2 * (a + b)
End Sub

PrevNext

 

 

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
02.05.2010