Guest User

Untitled

a guest
Jun 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "cstdlib"
  3. #include "ctime"
  4. #include "iostream"
  5. #include "iomanip"
  6. #include "locale.h"
  7.  
  8. using namespace std;
  9.  
  10. double** generate_random_matrix(int n, int random_max = 100)
  11. {
  12. srand(time(nullptr));
  13.  
  14. double** a = new double*[n];
  15. for (int i = 0; i < n; i++)
  16. {
  17. a[i] = new double[n];
  18. for (int j = 0; j < n; j++)
  19. {
  20. a[i][j] = rand() % random_max;
  21. }
  22. }
  23.  
  24. return a;
  25. }
  26. ////
  27. void smoothing_matrix(double** const src, int n)
  28. {
  29. int **dst = new int*[n];
  30. for (int i = 0; i < i; i++) {
  31. int cnt = 0;
  32. int sum = 0;
  33. for (int j = 0; j < n; j++) {
  34. if((i+1)<n){
  35. sum += src[i + 1][j];
  36. cnt++;
  37. }
  38. if ((j + 1)<n) {
  39. sum += src[i][j + 1];
  40. cnt++;
  41. }
  42. if ((i - 1)>=0) {
  43. sum += src[i + 1][j];
  44. cnt++;
  45. }
  46. if ((j - 1) >= 0) {
  47. sum += src[i][j + 1];
  48. cnt++;
  49. }
  50. dst[i][j] = sum / cnt;
  51.  
  52. }
  53. }
  54.  
  55. cout << "Сглаженная матрица: " << endl;
  56. for (int i = 0; i < n; i++) {
  57. for (int j = 0; j < n; j++) {
  58. cout << dst[i][j] << " ";//
  59. }
  60. cout << endl;
  61. }
  62.  
  63. }
  64. ////
  65. double sum_under_diag(double** const a, int const n)
  66. {
  67. double sum = .0;
  68.  
  69. for (int i = 0; i < n; i++)
  70. {
  71. for (int j = 0; j < i; j++)
  72. {
  73. sum += abs(a[i][j]);
  74. }
  75. }
  76. return sum;
  77. }
  78.  
  79. void output_matrix(double** const a, int const n)
  80. {
  81. for (int i = 0; i < n; i++)
  82. {
  83. for (int j = 0; j < n; j++)
  84. {
  85. cout << setw(10) << a[i][j];
  86. }
  87. cout << endl;
  88. }
  89. }
  90.  
  91. int main()
  92. {
  93. setlocale(LC_ALL, "Rus");
  94. int n;
  95. cout << "Введите размер матрицы:";
  96. cin >> n;
  97. if (n <= 2)
  98. {
  99.  
  100. cout << "\nВведен некорректный размер матрицы\n";
  101. cin.get(); cin.get();
  102. return 0;
  103. }
  104.  
  105. else
  106. {
  107. int m_size = n;
  108. int max_random = 100;
  109. cout.precision(3);
  110.  
  111. double** const source_matrix = generate_random_matrix(m_size, max_random);
  112. cout << "Матрица: " << endl;
  113. output_matrix(source_matrix, m_size);
  114.  
  115. //cout << "Сглаженная матрица: " << endl;
  116. smoothing_matrix(source_matrix, m_size);
  117.  
  118.  
  119. //output_matrix(source_matrix, m_size);
  120.  
  121. cout << "Сумма элементов под главной диагональю: " << sum_under_diag(source_matrix, m_size) << endl;
  122.  
  123. for (int i = 0; i < m_size; i++)
  124. delete[] source_matrix[i];
  125. delete[] source_matrix;
  126. }
  127. system("pause");
  128.  
  129.  
  130. return 0;
Add Comment
Please, Sign In to add comment