Programming Taskbook

Russian

E-mail:

Password:

User registration   Restore password

1000 training tasks on programming

©  M. E. Abramyan, 1998–2008

 

Solution of the simple task: Begin3

This section contains description of solving the following simple task in C++:

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.

We shall use Visual Studio 2003 as programming environment, however the same results may be received in the other C++ environments that are supported by Programming Taskbook: C++Builder version 4.0 or 5.0, Visual C++ 6.0, and Visual Studio 2005.

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 Studio 2003 consists of several files. But we need only Begin3.cpp file. This file will be loaded into the editor of Visual Studio:

#include <windows.h>
#pragma hdrstop
#include "pt4.h"

void Solve()
{
  Task("Begin3");
}

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

Remark. The Begin3.cpp file created in Visual C++ 6.0 and Visual Studio 2005 environments has the similar content. In C++Builder the Begin3.cpp file contains different proprocessor directives:

#include <vcl.h>
#pragma hdrstop
#include "pt4.h"
USEUNIT("pt4.cpp");

Note that the Begin3.cpp file also contains definition of the WinMain function, which calls the Solve function. This part of the program should remain unchanged, therefore we shall not analyse it.

Also note that a header file named pt4.h is included in the Begin3.cpp file. It contains declarations of additional types and functions, in particular, the Get–Put functions. Definitions of these functions are contained in the pt4.cpp file. This file is not loaded into the editor of Visual Studio. But the project includes the pt4.cpp file (see the Solution Explorer window located on the Visual Studio window).

To run the program press [F5] key (in the Borland C++Builder environment you should use [F9] key). Before compilation of the program the Visual Sudio environment may output the following dialog box

Select "Yes" (it is enough to press [Enter]). When the program is launched you will see the Programming Task winodow 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 (or [F9] key in the Borland C++Builder environment).

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 function:

void Solve()
{
  Task("Begin3");
  int a, b;
  GetN(a);
  GetN(b);
}

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

void Solve()
{
  Task("Begin3");
  double a, b;
  GetD(a);
  GetD(b);
}

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 function:

void Solve()
{
  Task("Begin3");
  double a, b;
  GetD(a);
  GetD(b);
  double S = a * b, P = 2 * (a + b);
  PutD(P);
  PutD(S);
}

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 function:

  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. With using the special pt stream we can make the program text more short:

void Solve()
{
  Task("Begin3");
  double a, b;
  pt >> a >> b;
  pt << a * b << 2 * (a + b);
}

 

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

Last revised: 11.05.2008