From c1a42b802c8d2eb94dc32acaf909459e13004f73 Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Tue, 6 Oct 2020 02:07:21 +0300 Subject: [PATCH 01/13] hw 1 --- HW_01.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 HW_01.cpp diff --git a/HW_01.cpp b/HW_01.cpp new file mode 100644 index 0000000..d4a3852 --- /dev/null +++ b/HW_01.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +constexpr int POWER{2}; +constexpr int MULTIPLIER{4}; + +char name = {'A'}; +double coefficient [3]{}; + +#define ARRAY_LEN(x) ( sizeof( coefficient ) / sizeof( coefficient[0] ) ) + +#define A coefficient [0] +#define B coefficient [1] +#define C coefficient [2] + +int main (void) { + std::cout<<"ax^2 + bx + c = 0"<"; + std::cin>>coefficient[i]; + + if ((coefficient[i] >= 2) && (coefficient[i] <= 255)) { + i++; + name++; + } else { + std::cout<<"You entered an incorrect number, try entering the number again"< 0) { + D = sqrt(D); + auto x = ((-B) - D) / (POWER * A); + std::cout<<"x1 = "<< x< Date: Tue, 6 Oct 2020 09:55:24 +0300 Subject: [PATCH 02/13] some mistakes was replaced --- HW_01.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HW_01.cpp b/HW_01.cpp index d4a3852..8ec8b0a 100644 --- a/HW_01.cpp +++ b/HW_01.cpp @@ -8,7 +8,7 @@ constexpr int MULTIPLIER{4}; char name = {'A'}; double coefficient [3]{}; -#define ARRAY_LEN(x) ( sizeof( coefficient ) / sizeof( coefficient[0] ) ) +#define ARRAY_LEN(x) ( sizeof( x ) / sizeof( x[0] ) ) #define A coefficient [0] #define B coefficient [1] @@ -43,7 +43,7 @@ int main (void) { } else if (D == 0) { auto x = (-B) / (POWER * A); std::cout<<"x = "< Date: Thu, 8 Oct 2020 23:24:33 +0300 Subject: [PATCH 03/13] fixes --- HW_01.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HW_01.cpp b/HW_01.cpp index 8ec8b0a..7ab9826 100644 --- a/HW_01.cpp +++ b/HW_01.cpp @@ -20,7 +20,7 @@ int main (void) { for (uint8_t i = 0; i < ARRAY_LEN(coefficient);) { std::cout<<"Please enter number in range [2..255]"<"; - std::cin>>coefficient[i]; + std::cin>>*coefficient; if ((coefficient[i] >= 2) && (coefficient[i] <= 255)) { i++; From 84880698505b3dd8c6ed8b351614d6f4e549eeb1 Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Wed, 14 Oct 2020 15:22:17 +0300 Subject: [PATCH 04/13] mistakes was corrected according code review --- HW_01.cpp | 160 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 120 insertions(+), 40 deletions(-) diff --git a/HW_01.cpp b/HW_01.cpp index 7ab9826..d97df45 100644 --- a/HW_01.cpp +++ b/HW_01.cpp @@ -1,53 +1,133 @@ -#include -#include #include +#include +#include +#include -constexpr int POWER{2}; -constexpr int MULTIPLIER{4}; +uint8_t equalation_init(double *a, double *b, double *c); -char name = {'A'}; -double coefficient [3]{}; +uint8_t equalation_solve(double a, double b, double c, double *x1, double *x2); -#define ARRAY_LEN(x) ( sizeof( x ) / sizeof( x[0] ) ) +void equalation_result_print(double a, double b, double c, uint8_t roots_num, + double x1, double x2); -#define A coefficient [0] -#define B coefficient [1] -#define C coefficient [2] +int main() { -int main (void) { - std::cout<<"ax^2 + bx + c = 0"<"; - std::cin>>*coefficient; + if (!equalation_init(&a, &b, &c)) { + return EXIT_FAILURE; + } - if ((coefficient[i] >= 2) && (coefficient[i] <= 255)) { - i++; - name++; - } else { - std::cout<<"You entered an incorrect number, try entering the number again"< 0) { - D = sqrt(D); - auto x = ((-B) - D) / (POWER * A); - std::cout<<"x1 = "<< x<"; + std::cin >> *init_array[i]; + + if ((*init_array[i] < LIM_MIN) || (*init_array[i] > LIM_MAX)) { + std::cout << "You entered an incorrect number!" << std::endl; + status = 0; + break; } + name++; + } + return status; +} - return 0; -} \ No newline at end of file +/** + * @brief This function solves the square equalation like + * Ax^2 + Ax + A = 0 + * + * @param a A coefficient + * @param b B coefficient + * @param c C coefficient + * @param x1 IO Poiter to 1st Root + * @param x2 IO Poiter to 2d Root + * @return uint8_t Result: 0 - no roots, 1 - one root, 2 - two roots (x1, x2) + */ +uint8_t equalation_solve(double a, double b, double c, double *x1, double *x2) { + uint8_t status = 0; + + if (!a || !b || !c || !x1 || !x2) { + return status; + } + + const int POWER{2}; + const int MULTIPLIER{4}; + auto D = pow(b, POWER) - (MULTIPLIER * a * c); + auto devider = POWER * a; + + if (D > 0) { + status = 2; + D = sqrt(D); + *x1 = ((-b) - D) / devider; + *x2 = ((-b) + D) / devider; + } else if (D == 0) { + status = 1; + *x1 = (-b) / (POWER * a); + } + return status; +} + +/** + * @brief This function prints the answer for the square equalation + * Ax^2 + Ax + A = 0 + * + * @param a A coefficient + * @param b B coefficient + * @param c C coefficient + * @param roots_num Quantity of roots + * @param x1 1st Root + * @param x2 2d Root + */ +void equalation_result_print(double a, double b, double c, uint8_t roots_num, + double x1, double x2) { + std::cout << "Result is :" << std::endl; + std::cout << a << "x^2 + " << b << "x + " << c << " = 0" << std::endl; + + if (roots_num == 2) { + std::cout << "x1 = " << x1 << ";" << std::endl; + std::cout << "x2 = " << x2 << ";" << std::endl; + } else if (roots_num == 1) { + std::cout << "x = " << x1 << ";" << std::endl; + } else { + std::cout << "equalation has no roots" << std::endl; + } +} From 7212170ced589d0f99fa514ac93483463483760d Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Wed, 14 Oct 2020 22:27:14 +0300 Subject: [PATCH 05/13] hw_02_01 --- HW_01.cpp | 133 --------------------------------------------------- HW_02_01.cpp | 45 +++++++++++++++++ 2 files changed, 45 insertions(+), 133 deletions(-) delete mode 100644 HW_01.cpp create mode 100644 HW_02_01.cpp diff --git a/HW_01.cpp b/HW_01.cpp deleted file mode 100644 index d97df45..0000000 --- a/HW_01.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include -#include -#include - -uint8_t equalation_init(double *a, double *b, double *c); - -uint8_t equalation_solve(double a, double b, double c, double *x1, double *x2); - -void equalation_result_print(double a, double b, double c, uint8_t roots_num, - double x1, double x2); - -int main() { - - double a{}; - double b{}; - double c{}; - - if (!equalation_init(&a, &b, &c)) { - return EXIT_FAILURE; - } - - double x1{}; - double x2{}; - - uint8_t roots = equalation_solve(a, b, c, &x1, &x2); - equalation_result_print(a, b, c, roots, x1, x2); - - return EXIT_SUCCESS; -} - -/** - * @brief This function creates an equalation with specific A,B,C coefficient - * Ax^2 + Bx + C = 0 - * - * @param a IO pointer to A coefficient - * @param b IO pointer to B coefficient - * @param c IO pointer to C coefficient - * @return uint8_t SUCCESS - 1, FAILURE - 0 - */ -uint8_t equalation_init(double *a, double *b, double *c) { - if (!a || !b || !c) { - return 0; - } - - uint8_t status = 1; - char name{'A'}; - const double LIM_MIN{2}; - const double LIM_MAX{255}; - const uint8_t INIT_ARRAY_SIZE{3}; - double *init_array[INIT_ARRAY_SIZE]{a, b, c}; - - std::cout << "Init a, b, c coefficients for next equalation:" << std::endl; - std::cout << "ax^2 + bx + c = 0" << std::endl; - - for (uint8_t i = 0; i < INIT_ARRAY_SIZE; i++) { - std::cout << "Please enter number in range [" << LIM_MIN << ".." << LIM_MAX - << "]" << std::endl; - std::cout << name << "-->"; - std::cin >> *init_array[i]; - - if ((*init_array[i] < LIM_MIN) || (*init_array[i] > LIM_MAX)) { - std::cout << "You entered an incorrect number!" << std::endl; - status = 0; - break; - } - - name++; - } - - return status; -} - -/** - * @brief This function solves the square equalation like - * Ax^2 + Ax + A = 0 - * - * @param a A coefficient - * @param b B coefficient - * @param c C coefficient - * @param x1 IO Poiter to 1st Root - * @param x2 IO Poiter to 2d Root - * @return uint8_t Result: 0 - no roots, 1 - one root, 2 - two roots (x1, x2) - */ -uint8_t equalation_solve(double a, double b, double c, double *x1, double *x2) { - uint8_t status = 0; - - if (!a || !b || !c || !x1 || !x2) { - return status; - } - - const int POWER{2}; - const int MULTIPLIER{4}; - auto D = pow(b, POWER) - (MULTIPLIER * a * c); - auto devider = POWER * a; - - if (D > 0) { - status = 2; - D = sqrt(D); - *x1 = ((-b) - D) / devider; - *x2 = ((-b) + D) / devider; - } else if (D == 0) { - status = 1; - *x1 = (-b) / (POWER * a); - } - return status; -} - -/** - * @brief This function prints the answer for the square equalation - * Ax^2 + Ax + A = 0 - * - * @param a A coefficient - * @param b B coefficient - * @param c C coefficient - * @param roots_num Quantity of roots - * @param x1 1st Root - * @param x2 2d Root - */ -void equalation_result_print(double a, double b, double c, uint8_t roots_num, - double x1, double x2) { - std::cout << "Result is :" << std::endl; - std::cout << a << "x^2 + " << b << "x + " << c << " = 0" << std::endl; - - if (roots_num == 2) { - std::cout << "x1 = " << x1 << ";" << std::endl; - std::cout << "x2 = " << x2 << ";" << std::endl; - } else if (roots_num == 1) { - std::cout << "x = " << x1 << ";" << std::endl; - } else { - std::cout << "equalation has no roots" << std::endl; - } -} diff --git a/HW_02_01.cpp b/HW_02_01.cpp new file mode 100644 index 0000000..11888a2 --- /dev/null +++ b/HW_02_01.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +uint32_t value_digits_quantity(uint32_t value); +uint32_t value_summ_digits(uint32_t value); + +int main() { + uint32_t value{}; + + std::cout << "Please, enter unsigned integer value" << std::endl; + std::cout << "-->"; + std::cin >> value; + + uint32_t quantity = value_digits_quantity(value); + + std::cout << "The value " << value << " consists of " << quantity << " digits" + << std::endl; + + uint32_t summ = value_summ_digits(value); + std::cout << "Summ of digits is " << summ << std::endl; + + auto average = static_cast(summ) / quantity; + std::cout << "The average is " << average << std::endl; +} + +uint32_t value_digits_quantity(uint32_t value) { + uint32_t quantity = 1; + + while (value /= 10) { + ++quantity; + } + + return quantity; +} + +uint32_t value_summ_digits(uint32_t value) { + uint32_t average = 0; + + for (; value; value /= 10) { + average += value % 10; + } + + return average; +} \ No newline at end of file From 4aee82f7cbe71dae624cac452c4a44515fa466cf Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Thu, 15 Oct 2020 01:13:23 +0300 Subject: [PATCH 06/13] HW_02_02 --- HW_02_02.cpp | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 HW_02_02.cpp diff --git a/HW_02_02.cpp b/HW_02_02.cpp new file mode 100644 index 0000000..f48d000 --- /dev/null +++ b/HW_02_02.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief This constant sets quantity of digits of ticket number that was entered by user + * + */ +constexpr uint32_t ALLOWWED_TICKET_DIGITS_QUANTITY{6}; + +/** + * @brief This constant defines a halves of ticket's nimber to check that ticket is lucky + * + */ +constexpr uint32_t TICKET_PART_TO_COMPARE{ALLOWWED_TICKET_DIGITS_QUANTITY / 2}; + +uint32_t value_digits_quantity(uint32_t value); +uint8_t value_summ_n_digits (uint32_t value, uint32_t quantity, uint32_t *summ); +uint8_t value_skip_n_digits (uint32_t* value, uint32_t n); + +int main() { + + uint32_t value{}; + + std::cout << "Please, enter unsigned integer value" << std::endl; + std::cout << "-->"; + std::cin >> value; + + uint32_t quantity = value_digits_quantity(value); + + std::cout << "The value " << value << " consists of " << quantity << " digits" + << std::endl; + + if (quantity != ALLOWWED_TICKET_DIGITS_QUANTITY) { + std::cout << "Value have to consist " << ALLOWWED_TICKET_DIGITS_QUANTITY << " digits!" + << std::endl; + return EXIT_FAILURE; + } + + uint32_t part1{}; + uint32_t part2{}; + + value_summ_n_digits(value, TICKET_PART_TO_COMPARE, &part1); + std::cout<< "part1: " << part1 << std::endl; + value_skip_n_digits(&value, TICKET_PART_TO_COMPARE); + std::cout<< "value: " << value << std::endl; + value_summ_n_digits(value, TICKET_PART_TO_COMPARE, &part2); + std::cout<< "part2: " << part2 << std::endl; + + /*Checking for ticket's lucky*/ + if (part1 == part2) { + std::cout<< "My congratulations, You have got a lucky ticket!"<< std::endl; + } else { + std::cout<< "No big deal, no luck with this ticket."<< std::endl; + } + + return EXIT_SUCCESS; +} + +/** + * @brief This function returns quantity of digits for given value + * + * @param value Input value for calculation + * @return uint32_t Quantity of digits + */ +uint32_t value_digits_quantity(uint32_t value) { + uint32_t quantity = 1; + + while (value /= 10) { + ++quantity; + } + + return quantity; +} + +/** + * @brief This function calculates summ of first n digits for given value + * + * @param value Input value for calculation + * @param quantity Quantity of digits for addition + * @param summ i/o pointer to variable, where summ will be written + * @return uint8_t SUCCESS - 1, FAILURE - 0 + */ +uint8_t value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ) { + + if (!summ || (value_digits_quantity(value) < quantity)) { + return 0; + } + + *summ = 0; + + while (quantity--) { + *summ += value % 10; + value /= 10; + } + + return 1; +} + +/** + * @brief This function trimms n first digits for given value + * + * @param value i/o pointer to variable for trimming + * @param n Number of first digits that will deleted from value + * @return uint8_t SUCCESS - 1, FAILURE - 0 + */ +uint8_t value_skip_n_digits (uint32_t* value, uint32_t n) { + if(!value) { + return 0; + } + + *value/=pow(10, n); + + return 1; +} \ No newline at end of file From 733080edb10f9f74dd2829efa8cf72dc688ad265 Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Thu, 15 Oct 2020 20:54:19 +0300 Subject: [PATCH 07/13] HW_02_03 --- HW_02_03.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 HW_02_03.cpp diff --git a/HW_02_03.cpp b/HW_02_03.cpp new file mode 100644 index 0000000..bcccd01 --- /dev/null +++ b/HW_02_03.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include + +uint32_t value_digits_quantity(uint32_t value); +bool value_trim_the_last_digit(uint32_t *value); +uint32_t value_get_the_last_digit(uint32_t value); + +int value_reverce(int value); + +int main() { + + int value{}; + + std::cout << "Please, enter integer value" << std::endl; + std::cout << "-->"; + std::cin >> value; + + int reverse = value_reverce(value); + + std::cout << "Reverse value is " << reverse << std::endl; + + return EXIT_SUCCESS; +} + +/** + * @brief This function returns quantity of digits for given value + * + * @param value Input value for calculation + * @return uint32_t Quantity of digits + */ +uint32_t value_digits_quantity(uint32_t value) { + uint32_t quantity = 1; + + while (value /= 10) { + ++quantity; + } + + return quantity; +} + +/** + * @brief This function returns the last digit for given value + * + * @param value [input] Value for calculation + * @return uint32_t The last digit + */ +uint32_t value_get_the_last_digit(uint32_t value) { return (value % 10); } + +/** + * @brief This function trims the last digit for given number + * + * @param value [input/output] Pointer to external variable + * @return true + * @return false + */ +bool value_trim_the_last_digit(uint32_t *value) { + if (!value) { + return false; + } + + *value /= 10; + + return true; +} + +/** + * @brief This function changes the order of given number + * + * @param value [input] Number for calculation + * @param order [input] New order for given number + * @return uint32_t Number with changed order + */ +uint32_t value_change_order(uint32_t value, uint32_t order) { + return static_cast(value * pow(10, order)); +} + +/** + * @brief This function returns value with reverse digits order + * + * @param value Value for calculation + * @return int Reverse value + */ +int value_reverce(int value) { + int reverse = 0; + bool is_signed = (value < 0); + // std::cout << "log: is signed = "<< is_signed << std::endl; + + uint32_t abs_value = static_cast(abs(value)); + // std::cout << "log: abs = "<< abs_value << std::endl; + + uint32_t value_order = value_digits_quantity(abs_value); + // std::cout << "log: order = "<< value_order << std::endl; + // std::cout << "---------------"<< std::endl; + + while (abs_value) { + auto last_digit = value_get_the_last_digit(abs_value); + // std::cout << "log: the last is = "<< last_digit << std::endl; + + reverse += value_change_order(last_digit, --value_order); + // std::cout << "log: reverse = "<< reverse << std::endl; + + value_trim_the_last_digit(&abs_value); + // std::cout << "log: abs = "<< abs_value << std::endl; + // std::cout << "---------------"<< std::endl; + } + + return is_signed ? -reverse : reverse; +} \ No newline at end of file From db63f83b2ad566639c5cf91efcee9ee6255da23f Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Sat, 17 Oct 2020 20:28:47 +0300 Subject: [PATCH 08/13] HW_02_04 was started --- HW_02_04.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 HW_02_04.cpp diff --git a/HW_02_04.cpp b/HW_02_04.cpp new file mode 100644 index 0000000..08ae01d --- /dev/null +++ b/HW_02_04.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include + +int summ_odd(); + +int main() { + /** + * @brief + * + */ + return EXIT_SUCCESS; +} + +int summ_odd() {} \ No newline at end of file From b2fb3e3fd87201d15eef4cf709abd4e6b56d3157 Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Sat, 17 Oct 2020 20:30:03 +0300 Subject: [PATCH 09/13] HW_02_04 was started --- HW_02_01.cpp | 22 ++++++++++++++++++---- HW_02_02.cpp | 26 ++++++++++++++------------ HW_02_03.cpp | 12 ++++++------ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/HW_02_01.cpp b/HW_02_01.cpp index 11888a2..37a0459 100644 --- a/HW_02_01.cpp +++ b/HW_02_01.cpp @@ -21,9 +21,17 @@ int main() { std::cout << "Summ of digits is " << summ << std::endl; auto average = static_cast(summ) / quantity; - std::cout << "The average is " << average << std::endl; + std::cout << "The average is " << average << std::endl; + + return EXIT_SUCCESS; } +/** + * @brief This function returns quantity of digits for given value + * + * @param value [input] Value for calculation + * @return uint32_t Quantity of digits + */ uint32_t value_digits_quantity(uint32_t value) { uint32_t quantity = 1; @@ -34,12 +42,18 @@ uint32_t value_digits_quantity(uint32_t value) { return quantity; } +/** + * @brief This function calculates summ for given value + * + * @param value [input] Given value for calculation + * @return uint32_t Summ + */ uint32_t value_summ_digits(uint32_t value) { - uint32_t average = 0; + uint32_t summ = 0; for (; value; value /= 10) { - average += value % 10; + summ += value % 10; } - return average; + return summ; } \ No newline at end of file diff --git a/HW_02_02.cpp b/HW_02_02.cpp index f48d000..86b7ad5 100644 --- a/HW_02_02.cpp +++ b/HW_02_02.cpp @@ -19,8 +19,8 @@ constexpr uint32_t ALLOWWED_TICKET_DIGITS_QUANTITY{6}; constexpr uint32_t TICKET_PART_TO_COMPARE{ALLOWWED_TICKET_DIGITS_QUANTITY / 2}; uint32_t value_digits_quantity(uint32_t value); -uint8_t value_summ_n_digits (uint32_t value, uint32_t quantity, uint32_t *summ); -uint8_t value_skip_n_digits (uint32_t* value, uint32_t n); +bool value_summ_n_digits (uint32_t value, uint32_t quantity, uint32_t *summ); +bool value_skip_n_digits (uint32_t* value, uint32_t n); int main() { @@ -64,7 +64,7 @@ int main() { /** * @brief This function returns quantity of digits for given value * - * @param value Input value for calculation + * @param value [input] Value for calculation * @return uint32_t Quantity of digits */ uint32_t value_digits_quantity(uint32_t value) { @@ -80,12 +80,13 @@ uint32_t value_digits_quantity(uint32_t value) { /** * @brief This function calculates summ of first n digits for given value * - * @param value Input value for calculation - * @param quantity Quantity of digits for addition - * @param summ i/o pointer to variable, where summ will be written - * @return uint8_t SUCCESS - 1, FAILURE - 0 + * @param value [input] Value for calculation + * @param quantity [input] Quantity of digits for addition + * @param summ [input/output] Pointer to variable, where summ will be written + * @return true + * @return false */ -uint8_t value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ) { +bool value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ) { if (!summ || (value_digits_quantity(value) < quantity)) { return 0; @@ -104,11 +105,12 @@ uint8_t value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ) { /** * @brief This function trimms n first digits for given value * - * @param value i/o pointer to variable for trimming - * @param n Number of first digits that will deleted from value - * @return uint8_t SUCCESS - 1, FAILURE - 0 + * @param value [input/output] pointer to variable for trimming + * @param n [input] Number of first digits that will deleted from value + * @return true + * @return false */ -uint8_t value_skip_n_digits (uint32_t* value, uint32_t n) { +bool value_skip_n_digits (uint32_t* value, uint32_t n) { if(!value) { return 0; } diff --git a/HW_02_03.cpp b/HW_02_03.cpp index bcccd01..60809f6 100644 --- a/HW_02_03.cpp +++ b/HW_02_03.cpp @@ -44,7 +44,7 @@ uint32_t value_digits_quantity(uint32_t value) { /** * @brief This function returns the last digit for given value - * + * * @param value [input] Value for calculation * @return uint32_t The last digit */ @@ -52,10 +52,10 @@ uint32_t value_get_the_last_digit(uint32_t value) { return (value % 10); } /** * @brief This function trims the last digit for given number - * + * * @param value [input/output] Pointer to external variable - * @return true - * @return false + * @return true + * @return false */ bool value_trim_the_last_digit(uint32_t *value) { if (!value) { @@ -69,7 +69,7 @@ bool value_trim_the_last_digit(uint32_t *value) { /** * @brief This function changes the order of given number - * + * * @param value [input] Number for calculation * @param order [input] New order for given number * @return uint32_t Number with changed order @@ -102,7 +102,7 @@ int value_reverce(int value) { reverse += value_change_order(last_digit, --value_order); // std::cout << "log: reverse = "<< reverse << std::endl; - + value_trim_the_last_digit(&abs_value); // std::cout << "log: abs = "<< abs_value << std::endl; // std::cout << "---------------"<< std::endl; From 87688966bf223a1e693438a6fecbf53506a7da5b Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Mon, 26 Oct 2020 23:09:28 +0200 Subject: [PATCH 10/13] HW_02_08 --- HW_02_04.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++++++---- HW_02_05.cpp | 39 ++++++++++++++++++++ HW_02_06.cpp | 36 +++++++++++++++++++ 3 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 HW_02_05.cpp create mode 100644 HW_02_06.cpp diff --git a/HW_02_04.cpp b/HW_02_04.cpp index 08ae01d..9def4d3 100644 --- a/HW_02_04.cpp +++ b/HW_02_04.cpp @@ -4,14 +4,102 @@ #include #include -int summ_odd(); +constexpr int MAX_ARRAY_SIZE{50}; +constexpr int MIN_ARRAY_SIZE{1}; + +bool is_odd(int value); +int array_sum(const int (&array)[MAX_ARRAY_SIZE], uint32_t size); +bool array_odd_init(int (&array)[MAX_ARRAY_SIZE], uint32_t &new_size, + uint32_t quantity); int main() { - /** - * @brief - * - */ + int array[MAX_ARRAY_SIZE]{}; + uint32_t array_size{}; + uint32_t entries{}; + + std::cout << "Enter the quantity of entries to process:" << std::endl; + std::cout << "Hint: Value should be in range [" << MIN_ARRAY_SIZE << "..." + << MAX_ARRAY_SIZE << "]" << std::endl; + std::cout << "-->"; + std::cin >> entries; + + if ((entries < MIN_ARRAY_SIZE) || (entries > MAX_ARRAY_SIZE)) { + std::cout << "ERROR: Incorrect entry" << std::endl; + return EXIT_FAILURE; + } + + std::cout << "Input" << std::endl; + if (!array_odd_init(array, array_size, entries)) { + return EXIT_FAILURE; + } + + int sum{array_sum(array, array_size)}; + + std::cout << sum << " --> " << array[0]; + + for (uint32_t i = 1; i < array_size; i++) { + std::cout << " + "; + std::cout << array[i]; + } + + std::cout << " = " << sum << std::endl; + return EXIT_SUCCESS; } -int summ_odd() {} \ No newline at end of file +/** + * @brief This function check value for odd condition + * + * @param value value to process + * @return true + * @return false + */ +bool is_odd(int value) { return value & 1; } + +/** + * @brief This function writes only odd numbers into array + * + * @param array [input/output] Array of references to process + * @param new_size [input/output] Refferense to variable that stores quantity + * of odd numbers + * @param quantity Quantity of entries + * @return true + * @return false + */ +bool array_odd_init(int (&array)[MAX_ARRAY_SIZE], uint32_t &new_size, + uint32_t quantity) { + if (quantity == 0) { + return false; + } + + constexpr int LIM_MIN{-60}; + constexpr int LIM_MAX{90}; + + new_size = 0; + + for (uint32_t i = 0; i < quantity; i++) { + std::cin >> array[new_size]; + + if ((array[new_size] < LIM_MIN) || (array[new_size] > LIM_MAX) || + !is_odd(array[new_size])) { + continue; + } + new_size++; + } + + return new_size; +} + +int array_sum(const int (&array)[MAX_ARRAY_SIZE], uint32_t size) { + if (size == 0) { + return 0; + } + + int sum{}; + + while (size--) { + sum += array[size]; + } + + return sum; +} \ No newline at end of file diff --git a/HW_02_05.cpp b/HW_02_05.cpp new file mode 100644 index 0000000..a37c366 --- /dev/null +++ b/HW_02_05.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +uint32_t value_best_devider(uint32_t value); + +int main() { + uint32_t value{}; + std::cout << "Input unsigned integer value:" << std::endl; + std::cout << "-->"; + std::cin >> value; + + auto devider = value_best_devider(value); + + if (devider == 0) { + std::cout << "ERROR: 0 has not devider!" << std::endl; + return EXIT_FAILURE; + } + + std::cout << "The best devider of value " << value << " is " << devider + << std::endl; + + return EXIT_SUCCESS; +} + +uint32_t value_best_devider(uint32_t value) { + if (value == 0) { + return 0; + } + + auto devider{value}; + + while (value % --devider) { + } + + return devider; +} \ No newline at end of file diff --git a/HW_02_06.cpp b/HW_02_06.cpp new file mode 100644 index 0000000..411cf52 --- /dev/null +++ b/HW_02_06.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +bool is_odd(int value); +// bool draw_triangle(uint32_t value); + +int main() { + + +} + +/** + * @brief This function check value for odd condition + * + * @param value value to process + * @return true + * @return false + */ +bool is_odd(int value) { return value & 1; } + +bool draw_triangle(char (&triangle) [31] [31], uint8_t filling) { + if (filling < 3) { + return false; + } + + filling = !is_odd(filling) ? ++filling : filling; + + return true; +} + +// while (value>>=1) { +// counter += (value & 1); +// } \ No newline at end of file From 049237f9b12e70296d71971bbcfd0aa5ff78ed3e Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Mon, 26 Oct 2020 23:09:58 +0200 Subject: [PATCH 11/13] HW_02_08 --- HW_02_01.cpp | 8 ++-- HW_02_02.cpp | 58 +++++++++++++------------- HW_02_03.cpp | 4 +- HW_02_06.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++----- HW_02_07.cpp | 65 ++++++++++++++++++++++++++++++ HW_02_08.cpp | 38 +++++++++++++++++ HW_02_09.cpp | 12 ++++++ 7 files changed, 254 insertions(+), 43 deletions(-) create mode 100644 HW_02_07.cpp create mode 100644 HW_02_08.cpp create mode 100644 HW_02_09.cpp diff --git a/HW_02_01.cpp b/HW_02_01.cpp index 37a0459..0cab536 100644 --- a/HW_02_01.cpp +++ b/HW_02_01.cpp @@ -22,19 +22,19 @@ int main() { auto average = static_cast(summ) / quantity; std::cout << "The average is " << average << std::endl; - + return EXIT_SUCCESS; } /** * @brief This function returns quantity of digits for given value - * + * * @param value [input] Value for calculation * @return uint32_t Quantity of digits */ uint32_t value_digits_quantity(uint32_t value) { uint32_t quantity = 1; - + while (value /= 10) { ++quantity; } @@ -44,7 +44,7 @@ uint32_t value_digits_quantity(uint32_t value) { /** * @brief This function calculates summ for given value - * + * * @param value [input] Given value for calculation * @return uint32_t Summ */ diff --git a/HW_02_02.cpp b/HW_02_02.cpp index 86b7ad5..1438aa8 100644 --- a/HW_02_02.cpp +++ b/HW_02_02.cpp @@ -1,26 +1,28 @@ #include #include +#include #include #include #include -#include #include /** - * @brief This constant sets quantity of digits of ticket number that was entered by user - * + * @brief This constant sets quantity of digits of ticket number that was + * entered by user + * */ constexpr uint32_t ALLOWWED_TICKET_DIGITS_QUANTITY{6}; /** - * @brief This constant defines a halves of ticket's nimber to check that ticket is lucky - * + * @brief This constant defines a halves of ticket's nimber to check that ticket + * is lucky + * */ constexpr uint32_t TICKET_PART_TO_COMPARE{ALLOWWED_TICKET_DIGITS_QUANTITY / 2}; uint32_t value_digits_quantity(uint32_t value); -bool value_summ_n_digits (uint32_t value, uint32_t quantity, uint32_t *summ); -bool value_skip_n_digits (uint32_t* value, uint32_t n); +bool value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ); +bool value_skip_n_digits(uint32_t *value, uint32_t n); int main() { @@ -36,8 +38,8 @@ int main() { << std::endl; if (quantity != ALLOWWED_TICKET_DIGITS_QUANTITY) { - std::cout << "Value have to consist " << ALLOWWED_TICKET_DIGITS_QUANTITY << " digits!" - << std::endl; + std::cout << "Value have to consist " << ALLOWWED_TICKET_DIGITS_QUANTITY + << " digits!" << std::endl; return EXIT_FAILURE; } @@ -45,17 +47,18 @@ int main() { uint32_t part2{}; value_summ_n_digits(value, TICKET_PART_TO_COMPARE, &part1); - std::cout<< "part1: " << part1 << std::endl; + std::cout << "part1: " << part1 << std::endl; value_skip_n_digits(&value, TICKET_PART_TO_COMPARE); - std::cout<< "value: " << value << std::endl; + std::cout << "value: " << value << std::endl; value_summ_n_digits(value, TICKET_PART_TO_COMPARE, &part2); - std::cout<< "part2: " << part2 << std::endl; + std::cout << "part2: " << part2 << std::endl; /*Checking for ticket's lucky*/ if (part1 == part2) { - std::cout<< "My congratulations, You have got a lucky ticket!"<< std::endl; + std::cout << "My congratulations, You have got a lucky ticket!" + << std::endl; } else { - std::cout<< "No big deal, no luck with this ticket."<< std::endl; + std::cout << "No big deal, no luck with this ticket." << std::endl; } return EXIT_SUCCESS; @@ -63,7 +66,7 @@ int main() { /** * @brief This function returns quantity of digits for given value - * + * * @param value [input] Value for calculation * @return uint32_t Quantity of digits */ @@ -79,12 +82,13 @@ uint32_t value_digits_quantity(uint32_t value) { /** * @brief This function calculates summ of first n digits for given value - * + * * @param value [input] Value for calculation * @param quantity [input] Quantity of digits for addition - * @param summ [input/output] Pointer to variable, where summ will be written - * @return true - * @return false + * @param summ [input/output] Pointer to variable, where summ will be + * written + * @return true + * @return false */ bool value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ) { @@ -104,18 +108,18 @@ bool value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ) { /** * @brief This function trimms n first digits for given value - * + * * @param value [input/output] pointer to variable for trimming - * @param n [input] Number of first digits that will deleted from value - * @return true - * @return false + * @param n [input] Number of first digits that will deleted from value + * @return true + * @return false */ -bool value_skip_n_digits (uint32_t* value, uint32_t n) { - if(!value) { - return 0; +bool value_skip_n_digits(uint32_t *value, uint32_t n) { + if (!value) { + return 0; } - *value/=pow(10, n); + *value /= pow(10, n); return 1; } \ No newline at end of file diff --git a/HW_02_03.cpp b/HW_02_03.cpp index 60809f6..27e9e84 100644 --- a/HW_02_03.cpp +++ b/HW_02_03.cpp @@ -6,7 +6,7 @@ #include uint32_t value_digits_quantity(uint32_t value); -bool value_trim_the_last_digit(uint32_t *value); +bool value_trim_the_last_digit(uint32_t *const value); uint32_t value_get_the_last_digit(uint32_t value); int value_reverce(int value); @@ -57,7 +57,7 @@ uint32_t value_get_the_last_digit(uint32_t value) { return (value % 10); } * @return true * @return false */ -bool value_trim_the_last_digit(uint32_t *value) { +bool value_trim_the_last_digit(uint32_t *const value) { if (!value) { return false; } diff --git a/HW_02_06.cpp b/HW_02_06.cpp index 411cf52..6ade4f1 100644 --- a/HW_02_06.cpp +++ b/HW_02_06.cpp @@ -1,15 +1,55 @@ +#include #include #include #include #include #include -bool is_odd(int value); -// bool draw_triangle(uint32_t value); +/** + * @brief Limitations on triangle sizes + * + */ +static constexpr uint32_t MATRIX_SIZE_MAX{41}; +static constexpr uint32_t MATRIX_SIZE_MIN{2}; + +typedef uint8_t matrix_t[MATRIX_SIZE_MAX]; + +/** + * @brief Space character ASCII code ( hex ) + * + */ +#define SPACE ((uint8_t)0x20) + +inline static bool is_odd(int value); +static bool triangle_init(uint32_t *const width, uint32_t *const height); +static bool triangle_draw(matrix_t *triangle, uint32_t width, uint32_t height); int main() { + matrix_t object[MATRIX_SIZE_MAX]{}; + uint32_t width{}; + uint32_t height{}; + + std::cout << "To draw an isosceles triangle, set its side as an unsigned value:" << std::endl; + std::cout << "Hint: the value has to be in range [" << MATRIX_SIZE_MIN << " ... " << MATRIX_SIZE_MAX << "]" << std::endl; + std::cout << "-->"; + std::cin >> width; + if (!triangle_init(&width, &height)) { + std::cout << "ERROR: Input value is not correct" << std::endl; + return EXIT_FAILURE; + } + + triangle_draw(object, width, height); + + for (uint32_t i = 0; i < height; i++) { + for (uint32_t j = 0; j < width; j++) { + std::cout << object[i][j]; + } + std::cout << std::endl; + } + + return EXIT_SUCCESS; } /** @@ -19,18 +59,70 @@ int main() { * @return true * @return false */ -bool is_odd(int value) { return value & 1; } +inline bool is_odd(int value) { return value & 1; } + +/** + * @brief The function calculates the dimensions of the matrix required to draw the triangle + * + * @param width [input/output] pointer to width propery + * @param height [input/output] pointer to calculate height propery + * @return true + * @return false + */ +static bool triangle_init(uint32_t *const width, uint32_t *const height) { -bool draw_triangle(char (&triangle) [31] [31], uint8_t filling) { - if (filling < 3) { + if ((*width < MATRIX_SIZE_MIN) || (*width > MATRIX_SIZE_MAX)) { return false; } - filling = !is_odd(filling) ? ++filling : filling; - + /*If side value is even - add 1 to align triangle*/ + if (!is_odd(*width)) { + (*width)++; + } + + /*Calculating*/ + uint32_t temp = *width; + *height = MATRIX_SIZE_MIN; + + do { + (*height)++; + temp -= 2; + } while (temp != 1); + return true; } -// while (value>>=1) { -// counter += (value & 1); -// } \ No newline at end of file +/** + * @brief This function draws the triangle + * + * @param triangle [input/output] pointer to matrix where triangle will be driven + * @param width matrix width + * @param height matrix heigth + * @return true + * @return false + */ +static bool triangle_draw(matrix_t *triangle, uint32_t width, uint32_t height) { + + if ((triangle == nullptr) || (width == 0) || (height == 0)) { + return false; + } + + /*filling array the space characters*/ + for (uint32_t i = 0; i < height; i++) { + for (uint32_t j = 0; j < width; j++) { + triangle[i][j] = SPACE; + } + } + + /*Drawing the triangle*/ + uint32_t swipe{}; + + while (--height) { + for (uint32_t col = swipe; col < (width - swipe); col++) { + triangle[height][col] = '*'; + } + swipe++; + } + + return true; +} \ No newline at end of file diff --git a/HW_02_07.cpp b/HW_02_07.cpp new file mode 100644 index 0000000..ac0964b --- /dev/null +++ b/HW_02_07.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include + +static uint32_t value_count_set_bits(uint32_t value); +static bool value_dec_2_bin(uint32_t value, char *string, uint32_t size); + +int main() { + uint32_t value{}; + constexpr uint32_t BIT_MASK_SIZE{sizeof(uint32_t) * 8}; + char BIT_MASK[BIT_MASK_SIZE]{}; + + std::cout << "Input unsigned integer value:" << std::endl; + std::cout << "-->"; + std::cin >> value; + + if (!value_dec_2_bin(value, BIT_MASK, BIT_MASK_SIZE)) { + return EXIT_FAILURE; + } + + for (uint32_t i = 0; i < BIT_MASK_SIZE; i++) { + if (((i % 4) == 0) && (i > 0)) { + std::cout << '\''; + } + std::cout << BIT_MASK[i]; + } + std::cout << std::endl; + + auto set_bist = value_count_set_bits(value); + + std::cout << set_bist << " bit is set." << std::endl; + + return EXIT_SUCCESS; +} + +static uint32_t value_count_set_bits(uint32_t value) { + uint32_t count{}; + + while (value) { + count += value & 1; + value >>= 1; + } + + return count; +} + +static bool value_dec_2_bin(uint32_t value, char *string, uint32_t size) { + if (string == nullptr) { + return false; + } + + for (uint32_t i = 0; i < size; i++){ + string[i] = '0'; + } + + while (value) { + string[--size] = '0' + (value & 1); + value >>= 1; + } + + return true; +} \ No newline at end of file diff --git a/HW_02_08.cpp b/HW_02_08.cpp new file mode 100644 index 0000000..41bed32 --- /dev/null +++ b/HW_02_08.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +static inline bool bit_is_set(uint32_t value, uint32_t bit) ; + +int main() { + uint32_t value{}; + std::cout << "Input unsigned integer value:" << std::endl; + std::cout << "-->"; + std::cin >> value; + + uint32_t bit{}; + std::cout << "Input bit's number to check is set:" << std::endl; + std::cout << "-->"; + std::cin >> bit; + + if (bit < 0) { + std::cout << "ERROR: Incorrect entry" << std::endl; + return EXIT_FAILURE; + } + + const char* response = bit_is_set(value, bit) ? "Yes" : "No"; + std::cout << response << std::endl; + + return EXIT_SUCCESS; +} + +static inline bool bit_is_set(uint32_t value, uint32_t bit) { + if(bit == 0) { + return false; + } + + return value & (1 << --bit); +} \ No newline at end of file diff --git a/HW_02_09.cpp b/HW_02_09.cpp new file mode 100644 index 0000000..5c60648 --- /dev/null +++ b/HW_02_09.cpp @@ -0,0 +1,12 @@ +#include +#include +#include +#include +#include +#include + + +int main () { + + return EXIT_SUCCESS; +} \ No newline at end of file From 2c8d92552c9d90c07bb113fa9568a75c83d90538 Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Wed, 28 Oct 2020 03:21:23 +0200 Subject: [PATCH 12/13] HW_02_06 a bit changed --- HW_02_06.cpp | 138 ++++++++++++++------------------------------------- 1 file changed, 37 insertions(+), 101 deletions(-) diff --git a/HW_02_06.cpp b/HW_02_06.cpp index 6ade4f1..e3a6d69 100644 --- a/HW_02_06.cpp +++ b/HW_02_06.cpp @@ -2,127 +2,63 @@ #include #include #include +#include #include #include +#include -/** - * @brief Limitations on triangle sizes - * - */ -static constexpr uint32_t MATRIX_SIZE_MAX{41}; -static constexpr uint32_t MATRIX_SIZE_MIN{2}; - -typedef uint8_t matrix_t[MATRIX_SIZE_MAX]; - -/** - * @brief Space character ASCII code ( hex ) - * - */ -#define SPACE ((uint8_t)0x20) - -inline static bool is_odd(int value); -static bool triangle_init(uint32_t *const width, uint32_t *const height); -static bool triangle_draw(matrix_t *triangle, uint32_t width, uint32_t height); +inline static bool is_even(int value); int main() { - - matrix_t object[MATRIX_SIZE_MAX]{}; - uint32_t width{}; - uint32_t height{}; - - std::cout << "To draw an isosceles triangle, set its side as an unsigned value:" << std::endl; - std::cout << "Hint: the value has to be in range [" << MATRIX_SIZE_MIN << " ... " << MATRIX_SIZE_MAX << "]" << std::endl; + constexpr uint32_t MATRIX_SIZE_MAX{64}; + constexpr uint32_t MATRIX_SIZE_MIN{2}; + constexpr uint32_t EVEN_PADDING{3}; + constexpr uint32_t ODD_PADDING{2}; + + char object[MATRIX_SIZE_MAX]; + uint32_t value{}; + + std::cout + << "To draw an isosceles triangle, set its side as an unsigned value:" + << std::endl; + std::cout << "Hint: the value has to be in range [" << MATRIX_SIZE_MIN + << " ... " << MATRIX_SIZE_MAX << "]" << std::endl; std::cout << "-->"; - std::cin >> width; + std::cin >> value; - if (!triangle_init(&width, &height)) { - std::cout << "ERROR: Input value is not correct" << std::endl; + if ((value < MATRIX_SIZE_MIN) || (value > MATRIX_SIZE_MAX - EVEN_PADDING)) { return EXIT_FAILURE; } - triangle_draw(object, width, height); + value += is_even(value) ? EVEN_PADDING : ODD_PADDING; - for (uint32_t i = 0; i < height; i++) { - for (uint32_t j = 0; j < width; j++) { - std::cout << object[i][j]; + /*Filling the array space character*/ + for (uint32_t i = 0; i < value; i++) { + object[i] = ' '; + } + + /*In this loop draws triangle from top to bottom*/ + for (uint32_t swipe = value / 2; swipe != 0; swipe--) { + + /*drawing triangle row*/ + for (uint32_t j = swipe; j < (value - swipe); j++) { + object[j] = '*'; } - std::cout << std::endl; + /*Set NULL termitaned character to init string*/ + object[value - swipe] = '\0'; + + /*Just print this string*/ + std::cout << object << std::endl; } return EXIT_SUCCESS; } /** - * @brief This function check value for odd condition + * @brief This function check value for even condition * * @param value value to process * @return true * @return false */ -inline bool is_odd(int value) { return value & 1; } - -/** - * @brief The function calculates the dimensions of the matrix required to draw the triangle - * - * @param width [input/output] pointer to width propery - * @param height [input/output] pointer to calculate height propery - * @return true - * @return false - */ -static bool triangle_init(uint32_t *const width, uint32_t *const height) { - - if ((*width < MATRIX_SIZE_MIN) || (*width > MATRIX_SIZE_MAX)) { - return false; - } - - /*If side value is even - add 1 to align triangle*/ - if (!is_odd(*width)) { - (*width)++; - } - - /*Calculating*/ - uint32_t temp = *width; - *height = MATRIX_SIZE_MIN; - - do { - (*height)++; - temp -= 2; - } while (temp != 1); - - return true; -} - -/** - * @brief This function draws the triangle - * - * @param triangle [input/output] pointer to matrix where triangle will be driven - * @param width matrix width - * @param height matrix heigth - * @return true - * @return false - */ -static bool triangle_draw(matrix_t *triangle, uint32_t width, uint32_t height) { - - if ((triangle == nullptr) || (width == 0) || (height == 0)) { - return false; - } - - /*filling array the space characters*/ - for (uint32_t i = 0; i < height; i++) { - for (uint32_t j = 0; j < width; j++) { - triangle[i][j] = SPACE; - } - } - - /*Drawing the triangle*/ - uint32_t swipe{}; - - while (--height) { - for (uint32_t col = swipe; col < (width - swipe); col++) { - triangle[height][col] = '*'; - } - swipe++; - } - - return true; -} \ No newline at end of file +inline bool is_even(int value) { return (value % 2 == 0); } \ No newline at end of file From 70b355ae74734274791c97f684a42f3c7f2a37da Mon Sep 17 00:00:00 2001 From: padawanYoung Date: Sun, 1 Nov 2020 14:40:53 +0200 Subject: [PATCH 13/13] hw_02_09 --- HW_02_09.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 4 deletions(-) diff --git a/HW_02_09.cpp b/HW_02_09.cpp index 5c60648..4ca574f 100644 --- a/HW_02_09.cpp +++ b/HW_02_09.cpp @@ -1,12 +1,111 @@ #include #include #include +#include #include #include #include +#include +static uint32_t value_digits_quantity(uint32_t value); +static uint64_t value_change_order (uint32_t value, uint32_t order); -int main () { - - return EXIT_SUCCESS; -} \ No newline at end of file +int main() { + constexpr uint32_t MAX_ARRAY_SIZE{10}; + uint32_t array[MAX_ARRAY_SIZE]{}; + uint32_t value{}; + + std::cout << "Please, enter number of values to process" << std::endl; + std::cout << "Hint: The value must be int range [1..." << MAX_ARRAY_SIZE + << "]" << std::endl; + std::cout << "-->"; + std::cin >> value; + + if ((value == 0) || (value > MAX_ARRAY_SIZE)) { + return EXIT_FAILURE; + } + + uint64_t summ_check{}; + + for (size_t i = 0; i < value; i++) { + std::cout << "Enter " << i + 1 << " value: "; + std::cin >> array[i]; + std::cout << std::endl; + + summ_check += array[i]; + } + + /*Сheck for the possibility of building a number*/ + if ((summ_check % 3) != 0) { + std::cout << "NO" << std::endl; + return EXIT_FAILURE; + } + + uint64_t calc_value_order{}; + uint64_t calc_value{}; + + /*Calculatу order for new value*/ + for (size_t i = 0; i < value; i++) { + calc_value_order += value_digits_quantity(array[i]); + } + + while(true) { + bool flag{false}; + + for (size_t i = 0; i < value; i++) { + + if (array[i] != 0) { + flag = true; + const auto __order = value_digits_quantity(array[i]) - 1; + const auto __trim = value_change_order(1, __order); + auto __value = array[i]; + + if (__order != 0) { + /*Skip last n digits*/ + __value /= __trim; + } + /*Get the last digit*/ + __value %= 10; + + calc_value += value_change_order(__value, --calc_value_order); + array[i] -= __value * __trim; + } + } + + if (flag == false) { + break; + } + } + + std::cout << "YES" << std::endl; + std::cout << "Calculated value is --> " << calc_value << std::endl; + + return EXIT_SUCCESS; +} + +/** + * @brief This function returns quantity of digits for given value + * + * @param value [input] Value for calculation + * @return uint32_t Quantity of digits + */ +static uint32_t value_digits_quantity(uint32_t value) { + uint32_t quantity = 1; + + while (value /= 10) { + ++quantity; + } + + return quantity; +} + +/** + * @brief This function return value with new order + * + * @param value Value to process + * @param order Value's order that will return + * @return uint64_t + */ +static uint64_t value_change_order (uint32_t value, uint32_t order) { + return static_cast(value * pow(10, order)); +}