Tvor0zhok

Рекурсия V

Mar 3rd, 2021 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. const int INF = 10e8;
  6.  
  7. //функция-шаблон для ввода массива
  8. template <class Type>
  9. void create (int &n, int &m, Type ** &mas)
  10. {
  11. cout << "n = "; cin >> n;
  12. cout << "m = "; cin >> m;
  13.  
  14. mas = new Type *[n];
  15. for (int i = 0; i < n; ++i)
  16. mas[i] = new Type [m];
  17.  
  18. for (int i = 0; i < n; ++i)
  19. for (int j = 0; j < m; ++j)
  20. cin >> mas[i][j];
  21. }
  22.  
  23. //функция-шаблон, реализующая решение задачи
  24. template <class Type>
  25. Type norm (int n, int m, Type **mas)
  26. {
  27. Type ans = 0;
  28.  
  29. for (int j = 0; j < m; ++j)
  30. {
  31. Type maximum = -INF;
  32. for (int i = 0; i < n; ++i)
  33. maximum = max(maximum, mas[i][j]);
  34.  
  35. ans += maximum;
  36. }
  37.  
  38. return ans;
  39. }
  40.  
  41. //функция-шаблон для вывода массива на экран
  42. template <class Type>
  43. void print (int n, int m, Type **mas)
  44. {
  45. cout << "Матрица:\n";
  46.  
  47. for (int i = 0; i < n; ++i, cout << '\n')
  48. for (int j = 0; j < m; ++j)
  49. cout << mas[i][j] << '\t';
  50. }
  51.  
  52. //функция-шаблон удаления массива
  53. template <class Type>
  54. void del (int n, Type **mas)
  55. {
  56. for (int i = 0; i < n; ++i)
  57. delete[] mas[i];
  58.  
  59. delete[] mas;
  60. }
  61.  
  62. int main()
  63. {
  64. //массив с целочисленными элементами
  65. int n1, m1;
  66. int **alpha;
  67.  
  68. create(n1, m1, alpha); //создаем массив
  69.  
  70. print(n1, m1, alpha); //выводим массив на экран
  71.  
  72. cout << "Норма матрицы равна: " << norm(n1, m1, alpha) << '\n';
  73.  
  74. del(n1, alpha); //удаляем массив
  75.  
  76. //массив с вещественными элементами
  77. int n2, m2;
  78. double **beta;
  79.  
  80. create(n2, m2, beta); //создаем массив
  81.  
  82. print(n2, m2, beta); //выводим массив на экран
  83.  
  84. cout << "Норма матрицы равна: " << norm(n2, m2, beta) << '\n';
  85.  
  86. del(n2, beta); //удаляем массив
  87.  
  88. return 0;
  89. }
Add Comment
Please, Sign In to add comment