diff --git a/HW_02_01.cpp b/HW_02_01.cpp new file mode 100644 index 0000000..0cab536 --- /dev/null +++ b/HW_02_01.cpp @@ -0,0 +1,59 @@ +#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; + + 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 for given value + * + * @param value [input] Given value for calculation + * @return uint32_t Summ + */ +uint32_t value_summ_digits(uint32_t value) { + uint32_t summ = 0; + + for (; value; value /= 10) { + summ += value % 10; + } + + return summ; +} \ No newline at end of file diff --git a/HW_02_02.cpp b/HW_02_02.cpp new file mode 100644 index 0000000..1438aa8 --- /dev/null +++ b/HW_02_02.cpp @@ -0,0 +1,125 @@ +#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); +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() { + + 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 [input] Quantity of digits for addition + * @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) { + + 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 [input/output] pointer to variable for trimming + * @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; + } + + *value /= pow(10, n); + + return 1; +} \ No newline at end of file diff --git a/HW_02_03.cpp b/HW_02_03.cpp new file mode 100644 index 0000000..27e9e84 --- /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 *const 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 *const 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 diff --git a/HW_02_04.cpp b/HW_02_04.cpp new file mode 100644 index 0000000..9def4d3 --- /dev/null +++ b/HW_02_04.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include + +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() { + 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; +} + +/** + * @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..e3a6d69 --- /dev/null +++ b/HW_02_06.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +inline static bool is_even(int value); + +int main() { + 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 >> value; + + if ((value < MATRIX_SIZE_MIN) || (value > MATRIX_SIZE_MAX - EVEN_PADDING)) { + return EXIT_FAILURE; + } + + value += is_even(value) ? EVEN_PADDING : ODD_PADDING; + + /*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] = '*'; + } + /*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 even condition + * + * @param value value to process + * @return true + * @return false + */ +inline bool is_even(int value) { return (value % 2 == 0); } \ 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..4ca574f --- /dev/null +++ b/HW_02_09.cpp @@ -0,0 +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() { + 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)); +}