Polnochniy

Untitled

Feb 10th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. const int N = 3;
  5. const int P = 4;
  6. int summ(int number)
  7. {
  8. int sum = 0;
  9. int F = number;
  10. while (F > 0)
  11. {
  12. sum += F % 10;
  13. F = F / 10;
  14. }
  15. return sum;
  16. }
  17. void cinn(int a[N][P], int N, int P)
  18. {
  19. cout << "Введите элементы массива" << endl;
  20. for (int i = 0; i < N; i++)
  21. {
  22. for (int j = 0; j < P; j++)
  23. {
  24. cin >> a[i][j];
  25. }
  26. cout << endl;
  27. }
  28. }
  29. void coutt(int a[N][P], int N, int P)
  30. {
  31. cout << "Вывод элементов " << endl;
  32. for (int i = 0; i < N; i++)
  33. {
  34. for (int j = 0; j < P; j++)
  35. {
  36. cout << a[i][j] << " ";
  37. }
  38. cout << endl;
  39. }
  40. cout << endl;
  41. }
  42.  
  43. void summ_numbers(int a[N][P], int N, int P)
  44. {
  45. cout << "Сумма цифр для cтолбцов элементов " << endl;
  46. for (int j = 0; j < P; j++)
  47. {
  48. for (int i = 0; i < N; i++)
  49. {
  50.  
  51. cout << summ(a[i][j]) << " " << endl;
  52. }
  53. cout << endl;
  54. }
  55. }
  56. int sum_numbers_all_P(int a[N][P], int index_col, int Num_rows)
  57. {
  58. int summm = 0;
  59. for (int index_row = 0; index_row < Num_rows; index_row++)
  60. {
  61. summm = summm + summ(a[index_row][index_col]);
  62. }
  63. return summm;
  64. }
  65. void sort(int a[N][P], int N, int P)
  66. {
  67. for (int indexCol = 0; indexCol < P; indexCol++)
  68. {
  69. for (int indexCol2 = indexCol + 1; indexCol2 < P; indexCol2++)
  70. {
  71. for (int indexCol2 = indexCol + 1; indexCol2 < P; indexCol2++)
  72. {
  73. if (sum_numbers_all_P(a, indexCol, N) < sum_numbers_all_P(a, indexCol2, N))
  74. {
  75. for (int k = 0; k < N; k++)
  76. {
  77. swap(a[k][indexCol], a[k][indexCol2]);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. void rez(int a[N][P], int N, int P)
  85. {
  86. cout << "Результат " << endl;
  87. for (int i = 0; i < N; i++)
  88. {
  89. for (int j = 0; j < P; j++)
  90. {
  91. cout << a[i][j] << " ";
  92. }
  93. cout << endl;
  94. }
  95. cout << endl;
  96. }
  97. int main()
  98. {
  99. int a[N][P];
  100. setlocale(LC_ALL, "Rus");
  101. srand(time(NULL));
  102. cout << " Сортировать элементы стобцов по сумм цифр " << endl;
  103. cinn(a, N, P);
  104. coutt(a, N, P);
  105. summ_numbers(a, N, P);
  106. cout << "Общая сумма цифр для каждого столбца " << endl;
  107. for (int indexCol = 0; indexCol < P; indexCol++)
  108. {
  109. cout << sum_numbers_all_P(a, indexCol, N) << " " << endl;
  110. }
  111. sort(a, N, P);
  112. rez(a, N, P);
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment