Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University), 1998–2018

 

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 Begin3.bas file. This file will be loaded into the IDE being used:

Option Explicit
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 pt4main file (with the .bas extension) that was created when the project was generated for this task. The pt4main file is not loaded into the code editor of the IDE. But the project includes this file (see the Project window in the Visual Basic 5-6 IDE).

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:

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." Note that each type of errors is marked by different color.

To input data correctly we should use the GetD procedure that provides input data of Double type. Change our program as follows:

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. In such situation we have the following message: "Correct data input: all required data are input, no data are output" (Programming Taskbook version 4.15 or higher) or "Some data are not output" (previous versions).

Calculation and output results

Let's perform the required calculations and output results using the PutD procedure:

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

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      b23/02 20:13 Acquaintance with the task.
Begin3      b04/02 15:07 Invalid type is used for an input data item.
Begin3      b04/02 15:09 Correct data input.
Begin3      b04/02 15:12 Wrong solution.
Begin3      b04/02 15:15 The task is solved!

The letter "b" denotes the programming language being used (Visual Basic).

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

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

 

  Рейтинг@Mail.ru

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

Last revised:
06.05.2018