Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU SMBU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2024

 

Examples | C and C++ | Simple task

PrevNext


Solution of a simple task: Begin3

This section contains description of solving the following simple task in C and 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 use the Visual Studio Code as an IDE, however, similar results will be obtained when developing the program in other environments supported by the taskbook for C and C++ languages.

Creating a template and acquaintance with the task

To create a template of the required task you should use the PT4Load tool (starting with version 4.22, you can use the PT4Panel tool for quick launch of all Programming Taskbook tools; the PT4Panel shortcut is located on the desktop and in any working directory).

The template created for Visual Studio Code consists of several files. But we need only the Begin3 file with the extension .c in the case of the C language and the extension .cpp for the C++ language. It is this file that will be opened in the Visual Studio Code IDE editor. Here is the text of this file:

[Begin3.c]

#include "pt4.h"

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

[Begin3.cpp]

#include "pt4.h"
using namespace std;

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.

Note that this file does not contain a description of the main function, which calls the Solve function. Since the main function does not need to be changed when solving the task, it has been moved to the header file pt4.h included in the Begin3.c and Begin3.cpp files.

The header file pt4.h also contains declarations of additional types and functions, in particular, the Get–Put functions. Definitions of these functions are contained in the pt4.c and pt4.cpp files.

To run the program, press [F5] in the Visual Studio Code IDE or Microsoft Visual Studio IDE, [F9] in the Code::Blocks IDE, [F11] in the Dev-C++ IDE.

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, since the program does not perform input-output operations. To close the Programming Taskbook window, click the Exit button or press [Esc] or the key that was used to launch the program ([F5] for the Visual Studio Code and Microsoft Visual Studio IDEs, [F9] for the Code::Blocks IDE, [F11] for the Dev-C++ IDE).

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 us model this situation in our program. For this purpose, we perform input to variables of the integer type using the GetN function and add the text of the function Solve as follows (we will not specify the header of the Solve function):

[C/C++]

Task("Begin3");
int a, b;
GetN(&a);
GetN(&b);

Note that in the Get functions for the C language (and also for the C++ language, starting with the taskbook version 4.23), you must specify the address of the variable in which the next element of the source data will be written. The & operator is used to get the address.

Remark. In the versions of the taskbook preceding version 4.23, the Get functions for the C++ language use not addresses as parameters, but the variables themselves, passed as references. Since there is no refernces in the C language, in the taskbook version 4.23 (the first version that supports both the C and C++ languages), the Get functions are implemented in the same way - with parameters of the pointer type.

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 any valid set of initial data.


In this case, the running leads to the following message in the information panel: "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 function that provides input data of double type. Change our program as follows:

[C/C++]

Task("Begin3");
double a, b;
GetD(&a);
GetD(&b);

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 us perform the required calculations and output results using the PutD function:

[C/C++]

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 information panel 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:

[C/C++]

PutD(S);
PutD(P);

After launching the corrected program, a test progress window will appear on the screen:

This window 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 should close the testing panel by means of the [x] button in its upper right corner.

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

The letter "c" indicated before the date means that the task was solved in the C++ language. For the C language, the capital letter is used: "C".

Remark 1. To output the results, it is not necessary to save them in special variables beforehand. Here is the appropriate solution:

[C/C++]

Task("Begin3_ru");
double a, b;
GetD(&a);
GetD(&b);
PutD(a * b);
PutD(2 * (a + b));

Remark 2. When solving a task in C++, you can use additional nput-output tools that allow you to arrange input and output in a more compact way. In particular, you can use a special input-output stream pt, defined in the file pt4.cpp:

[C++]

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

Since version 4.15, the pt stream can be used in conjunction with iterators ptin_iterator<T> и ptout_iterator<T> to arrange input and output sequences.

Since version 4.22, one can also use the GetBool, GetInt, GetDouble, GetChar and GetString functions to input data. Applying the GetDouble function, we can get a Begin3 solution consisting of just two statements:

[C++]

Task("Begin3");
double a = GetDouble(), b = GetDouble();
pt << a * b << 2 * (a + b);

PrevNext

 

  Рейтинг@Mail.ru

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

Last revised:
01.01.2024