35657

Untitled

Apr 23rd, 2023
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. //============================================Задание 1=================================================================
  7. // Написать функцию, добавляющую столбец двумерному массиву по указанному индексу.
  8.  
  9.  
  10. void AddColumn(int** arr, int lines, int& cols, int index) {
  11.     for (int i = 0; i < lines; ++i) {
  12.         int* temp = new int[cols + 1];
  13.         for (int j = 0; j < index; j++) {
  14.             temp[j] = arr[i][j];
  15.         }
  16.         temp[index] = 0;
  17.         for (int j = index; j < cols; j++) {
  18.             temp[j + 1] = arr[i][j];
  19.         }
  20.         delete[] arr[i];
  21.         arr[i] = temp;
  22.     }
  23.     cols++;
  24. }
  25.  
  26. int main()
  27. {
  28.     int lines = 5;
  29.     int cols = 5;
  30.     int** arr = new int* [lines];
  31.     for (int i = 0; i < lines; i++) {
  32.         arr[i] = new int[cols];
  33.     }
  34.  
  35.     for (int i = 0; i < lines; i++) {
  36.         for (int j = 0; j < cols; j++) {
  37.             arr[i][j] = j + i * cols;
  38.         }
  39.     }
  40.  
  41.  
  42.     for (int i = 0; i < lines; i++) {
  43.         for (int j = 0; j < cols; j++) {
  44.             cout << setw(4) << arr[i][j];
  45.         }
  46.         cout << std::endl;
  47.     }
  48.  
  49.     cout << std::endl;
  50.  
  51.     AddColumn(arr, lines, cols, 3);
  52.  
  53.     for (int i = 0; i < lines; i++) {
  54.         for (int j = 0; j < cols; j++) {
  55.             cout << setw(4) << arr[i][j];
  56.         }
  57.         cout << std::endl;
  58.     }
  59.  
  60.     for (int i = 0; i < lines; i++) {
  61.         delete[] arr[i];
  62.     }
  63.     delete[] arr;
  64. }
  65.  
  66. //============================================Задание 2=================================================================
  67. // Написать функцию, удаляющую столбец двумерного массива по указанному индексу.
  68.  
  69. void DelColumn(int** arr, int lines, int& cols, int index) {
  70.     for (int i = 0; i < lines; ++i) {
  71.         int* temp = new int[cols - 1];
  72.         for (int j = 0; j < index; j++) {
  73.             temp[j] = arr[i][j];
  74.         }
  75.         for (int j = index + 1; j < cols; j++) {
  76.             temp[j - 1] = arr[i][j];
  77.         }
  78.         delete[] arr[i];
  79.         arr[i] = temp;
  80.     }
  81.     cols--;
  82. }
  83.  
  84. int main()
  85. {
  86.     int lines = 5;
  87.     int cols = 5;
  88.     int** arr = new int* [lines];
  89.     for (int i = 0; i < lines; i++) {
  90.         arr[i] = new int[cols];
  91.     }
  92.  
  93.     for (int i = 0; i < lines; i++) {
  94.         for (int j = 0; j < cols; j++) {
  95.             arr[i][j] = j + i * cols;
  96.         }
  97.     }
  98.  
  99.  
  100.     for (int i = 0; i < lines; i++) {
  101.         for (int j = 0; j < cols; j++) {
  102.             cout << setw(4) << arr[i][j];
  103.         }
  104.         cout << std::endl;
  105.     }
  106.  
  107.     cout << std::endl;
  108.  
  109.     DelColumn(arr, lines, cols, 3);
  110.     cols--;
  111.  
  112.     for (int i = 0; i < lines; i++) {
  113.         for (int j = 0; j < cols; j++) {
  114.             cout << setw(4) << arr[i][j];
  115.         }
  116.         cout << std::endl;
  117.     }
  118.  
  119.     for (int i = 0; i < lines; i++) {
  120.         delete[] arr[i];
  121.     }
  122.     delete[] arr;
  123. }
  124.  
  125.  
  126. //============================================Задание 3=================================================================
  127. // Создайте структуру, описывающую комплексное число. Реализуйте арифметические операции с комплексными числами: сумму, разность, умножение, деление.
  128.  
  129.  
  130. struct complex_number {
  131.     double a;
  132.     double b;
  133. };
  134.  
  135. complex_number addition(const complex_number& x, const complex_number& y) {
  136.     return complex_number{ (x.a + y.a), (x.b + y.b) };
  137. }
  138.  
  139. complex_number deduction(const complex_number& x, const complex_number& y) {
  140.     return complex_number{ (x.a - y.a), (x.b - y.b) };
  141. }
  142.  
  143. complex_number multiplication(const complex_number& x, const complex_number& y) {
  144.     return complex_number{ (x.a * y.a - x.b * y.b), (x.a * y.b + y.a * x.b) };
  145. }
  146.  
  147. complex_number division(const complex_number& x, const complex_number& y) {
  148.     return complex_number{ ((x.a * y.a + x.b * y.b) / (y.a * y.a + y.b * y.b)),  ((y.a * x.b - x.a * y.b) / (y.a * y.a + y.b * y.b)) };
  149. }
  150.  
  151. int main() {
  152.     complex_number x = { 3,4 }, y = { 5,10 };
  153.     complex_number result;
  154.     result = addition(x, y);
  155.     cout << "addition: result.a = " << result.a << " result.b = " << result.b << endl;
  156.     result = deduction(x, y);
  157.     cout << "deduction: result.a = " << result.a << " result.b = " << result.b << endl;
  158.     result = multiplication(x, y);
  159.     cout << "multiplication: result.a = " << result.a << " result.b = " << result.b << endl;
  160.     result = division(x, y);
  161.     cout << "division: result.a = " << result.a << " result.b = " << result.b << endl;
  162. }
  163.  
  164.  
  165. //==================================================пример использования sizeof()================================
  166.  
  167. #include <iostream>
  168. using namespace std;
  169.  
  170. int main() {
  171.     char line1[20]{ "hello" };
  172.     char line2[20];
  173.     int arr1[20]{ 1,2,3,4 };
  174.     int arr2[20];
  175.     cout << "Размер массива: " << sizeof(line1) / sizeof(char) << "элементов" << endl;
  176.     cout << "Размер массива: " << sizeof(line2) / sizeof(char) << "элементов" << endl;
  177.     cout << "Размер массива: " << sizeof(arr1) / sizeof(int) << "элементов" << endl;
  178.     cout << "Размер массива: " << sizeof(arr2) / sizeof(int) << "элементов" << endl;
  179. }
  180.  
  181.  
  182. //==============================================пример использования auto / decltype====================================
  183.  
  184.  
  185. #include <iostream>
  186. using namespace std;
  187.  
  188. int minF() {
  189.     int a = 20;
  190.     int b = 10;
  191.     if (a < b) {
  192.         return a;
  193.     }
  194.     else {
  195.         return b;
  196.     }
  197. }
  198.  
  199. int myF()
  200. {
  201.     int a = 20;
  202.     return a + 5;
  203. }
  204.  
  205. int main() {
  206.  
  207.     // стандартная инициализация
  208.  
  209.     double myVar;
  210.     myVar = 4.2;
  211.     double myVar2 = 4.2;
  212.  
  213.     // варианты инициализации с auto
  214.  
  215.     auto myVar3 = 4.2;
  216.  
  217.     auto myExpr = 2 + 5;
  218.  
  219.     auto ptr = &myVar;
  220.  
  221.     auto myMin = minF();
  222.  
  223.     // варианты инициализации с decltype
  224.  
  225.     int x = 1;
  226.     decltype(x) j;
  227.     //cout << j << endl;
  228.  
  229.     int x2 = 1;  // аналогично тому что выше но в этом случае j2 сразу
  230.     auto j2 = x2;  // инициализирована значением, хранящимся в x2
  231.     //cout << j2 << endl;
  232.  
  233.     int y = 10;
  234.     int& r = y;
  235.     decltype(r) var = y; // var становится ссылкой на y
  236.     var = 12;
  237.     //cout << y << endl;
  238.  
  239.     int y2 = 10;
  240.     int& r2 = y2;
  241.     auto var2 = r2; // var становится переменной типа int
  242.     var = 12;
  243.     //cout << y2 << endl;
  244.  
  245.     decltype(myF()) varF = 3; // можно получить тип возвращаемого функцией значения
  246.     cout << varF << endl;
  247.  
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
Advertisement
Add Comment
Please, Sign In to add comment