- Data types, variables and constants
- Ввод с клавиатуры и вывод данных на экран
- Арифметические операции и комбинированные операторы
- Операторы выбора if и else в С
- The switch statement in C ++
- The for loop in C++
- Циклы while, do while, nested loops
- Two-dimensional arrays C ++
- Arrays in C++
- Functions in C++
- Pointers C ++
- A pointer to a pointer to C ++
- Classes in C ++
Additional collection of problems with solutions
This page contains all of the tasks on the topics lessons, posted on our website. К каждой задаче прилагается наш вариант решения. Постарайтесь всё же решать их самостоятельно и только потом сравнивать наш исходный код со своим. “Хочешь научиться программировать – программируй”!
Больше практических заданий с решениями найдёте here.
Рекомендуем посетить Сайт для юных программистов – где вы найдете уроки по различным языкам программирования (в том числе для детей), 3D-моделированию, Linux и др.
create functions, in which to transfer data, as parameters;
Remove all lines from the text file f, the length of which is less 3. Write the result to file g.
#include
#include
#include
#include
static std::vector read_file(std::string);
static void print_vector(std::vector);
static std::vector delete_lines(std::vector &, size_t);
static void save_file(std::string, std::vector);
int main()
{
std::string file_name;
std::cout <> file_name;
std::getchar();
std::vector all_lines = read_file(file_name);
print_vector(all_lines);
std::vector out_lines = delete_lines(all_lines, 3);
print_vector(out_lines);
std::cout <> file_name;
std::getchar();
std::getchar();
save_file(file_name, out_lines);
return 0;
}
static void save_file(std::string file_name, std::vector vect)
{
std::ofstream outfile;
outfile.open(file_name, std::ios::out);
if (!outfile.is_open())
{
std::cout << "error open file\n";
}
else
{
for (size_t i = 0; i < vect.size(); i )
{
outfile << vect[i] << std::endl;
}
}
outfile.close();
}
static std::vector delete_lines(std::vector & vect, size_t len)
{
static std::vector result_vect;
for (size_t i = 0; i len)
{
result_vect.push_back(vect[i]);
}
}
return result_vect;
}
static void print_vector(std::vector vect)
{
std::cout << "\n====================" << std::endl;
for (size_t i = 0; i < vect.size(); i )
{
std::cout << vect[i] << std::endl;
}
std::cout << "\n====================" << std::endl;
}
static std::vector read_file(std::string file_name)
{
std::string line;//Cannot read properties of undefined (reading ‘setAtlas’)
std::vector vect;
std::ifstream infile(file_name, std::ios::in);
if (!infile.is_open())
{
std::cout << "error read file\n";
}
while (!infile.eof())
{
std::getline(infile, line);
vect.push_back(line);
}
infile.close();
return vect;
}
Program, which along the vector A, forms the vector B, which will contain only even elements of the vector A
Identify, does point N belong(x;Y) triangle, formed by the axes AB,
AC and direct
y = 7.6x – 3.5
The ship can hold N kg of gold in the hold (N is read from the keyboard).
Conveyor window, which delivers gold bars to him has dimensions W = 50 cm (length) and H = 50 cm (height).
The user enters the dimensions from the keyboard (w,h,l) gold bars, which can be placed on the conveyor.
Ingots, which are larger than the conveyor window do not enter the hold and are not placed on the conveyor.
It is necessary to fill the ship's hold with gold bars to the maximum, but do not overload it.
Simulate the process of selecting gold bars, which enter the hold.
It is necessary to display on the screen:
– dimensions of gold bars and their weight.
– the need for their turns on the conveyor to get into the hold or the fact that, that the ingot does not fit.
– residual weight in the hold for gold (how many more kilograms can the hold hold).
The program is still running (or):
– The hold will not be full;
– Its occupancy will be less than the maximum at 10-20 kg you 3 ingots in a row did not go to the hold.
– The user will not enter a character, which stops the program execution.
Additionally: gold bars can be generated randomly with romirs from 1cm to 100cm, with:
– the output to the screen should remain the same.
– the number of ingots, which did not go to the hold in a row is at least 10.
1. File contains 100 whole numbers. count up, how many of them are there
threes and how many numbers are greater than ten.
2. File contains 50 real numbers. count up, how many of them are there
negative numbers (display). Output numbers to another file
positions, in which there are negative numbers and their values.
3. Write a program, reading from a file the results of some
experiment, calculating the average, absolute and relative
the measurement error and the result are added to this file
#include
#include
#include
#include
#include
const std::string FILE_NAME = “exp_021.bin”;
const size_t MAX_LEN = 100;
void fill_array(int *);
void print_array(int *);
bool save_array(int *, size_t , std::string);
bool load_array(int *, size_t , std::string);
size_t calculate_by_value(int *, int);
size_t calculate_by_value2(int *, int);
int main()
{
srand(time(nullptr));
int * int_array = new int[MAX_LEN];
fill_array(int_array);
print_array(int_array);
save_array(int_array, MAX_LEN, FILE_NAME);
load_array(int_array, MAX_LEN, FILE_NAME);
print_array(int_array);
std::cout << "\nthe number of numbers is equal 3 = " << calculate_by_value(int_array,3);
std::cout << "\nnumber of numbers greater than 10 = " << calculate_by_value2(int_array,10);
}
size_t calculate_by_value2(int * arr, int value)
{
size_t count = 0;
for (size_t i=0 ; i value)
{
count ;
}
}
return count;
}
size_t calculate_by_value(int * arr, int value)
{
size_t count = 0;
for (size_t i=0 ; i< MAX_LEN ; i )
{
if (arr[i] == value)
{
count ;
}
}
return count;
}
bool load_array(int * arr, size_t length , std::string file_name)
{
std::ifstream is(file_name, std::ios::binary | std::ios::in);
if ( !is.is_open() )
return false;
is.read(reinterpret_cast(arr), std::streamsize(length*sizeof(int)));
is.close();
return true;
}
bool save_array(int * arr, size_t length, const std::string file_name)
{
std::off stream us(file_name, std::ios::binary | std::ios::out);
if ( !os.is_open() )
return false;
os.write(reinterpret_cast(arr), std::streamsize(length*sizeof(int)));
os.close();
return true;
}
void print_array(int arr[])
{
std::cout << "\n==========================\n";
for (size_t i=0 ; i< MAX_LEN ; i )
{
std::cout << arr[i] << "\t";
if (i%7 == 0)
{
std::cout << std::endl;
}
}
std::cout << "\n==========================\n";
}
void fill_array(int arr[])
{
for (size_t i=0 ; i< MAX_LEN ; i )
arr[i] = int(rand());
}
#include
#include
#include
#include
#include
The Federation of Skating Sports decided on an unusual experiment. Now, instead of starting numbers, starting lines will be written on athletes' suits. It is planned, that ceo will increase the spectacle of the competition. Especially since the lines are planned to be generated randomly.
Formally, the generation of a line looks as follows. A string is defined by a sequence of random characters. Each character is generated independently from the first N letters of the Latin alphabet with equal probability and is added to the end of the existing string. The character is added to the string until then, until one of the specified patterns is found as a substring in the string.
It is necessary to find the mathematical expectation of the length of the generated string.
Incoming data
The numbers N and M are written in the first line (1 ≤ N ≤ 8, 1 ≤ M ≤ 10). N – the number of letters in the alphabet used. M is the number of templates. Next, M lines are recorded, which contain templates, consisting of the first N letters of the alphabet. The length of the templates does not exceed 10.
Output data
Output the mathematical expectation of the string length to two decimal places.
In some schools, each student has his own personal ICQ number. It is a common opinion at school, that the smaller the value of the ICQ number, all the more “promote” is a schoolboy. The list of all schoolchildren with ICQ numbers is known. We need to display the list of K ourselves “advanced” schoolchildren.
Incoming data
The first line contains the number of students in school N (1 ≤ N ≤ 100) and the number K (1 ≤ K ≤ N). Next comes N lines, each line contains the student's last name (without gaps, contains no more 20 linear Latin letters) and by skipping the ICQ number (1 ≤ ICQ ≤ 109). ICQ numbers and surnames of schoolchildren are different.
Output data
Display K's surnames themselves “advanced” students in lexicographical order (alphabetically). Each surname is displayed on a separate line.
Examples
Below you will find examples of input data and responses, that your program should output.