Advertisement
Polnochniy

Untitled

Jan 22nd, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 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.  
  7. int sum(int* a, int N)
  8. {
  9. int sum = 0;
  10. for (int i = 0; i < N; i++)
  11. {
  12. sum += a[i];
  13. }
  14. return sum;
  15. }
  16. void cinn(int a[N][P], int N, int P)
  17. {
  18. cout << "Введите элементы массива" << endl;
  19. for (int i = 0; i < N; i++)
  20. {
  21. for (int j = 0; j < P; j++)
  22. {
  23. cin >> a[i][j];
  24. }
  25. cout << endl;
  26. }
  27. }
  28. void coutt(int a[N][P], int N, int P)
  29. {
  30. cout << "Вывод элементов " << endl;
  31. for (int i = 0; i < N; i++)
  32. {
  33. for (int j = 0; j < P; j++)
  34. {
  35. cout << a[i][j] << " ";
  36. }
  37. cout << endl;
  38. }
  39. cout << endl;
  40. }
  41. void sum_row(int a[N][P], int N, int P)
  42. {
  43. cout << "Сумма элементов по строкам " << endl;
  44. for (int k = 0; k < N; k++)
  45. {
  46. cout << sum(a[k], P) << " " << endl;
  47. }
  48. cout << endl;
  49. }
  50. void sort(int a[N][P], int N, int P)
  51. {
  52. for (int i = 0; i < N - 1; i++)
  53. {
  54. for (int j = i + 1; j < N; j++)
  55. {
  56. if (sum(a[i], P) < sum(a[j], P))
  57. {
  58. swap(a[i], a[j]);
  59. }
  60. }
  61. }
  62. }
  63. void rez(int a[N][P], int N, int P)
  64. {
  65. cout << "Результат " << endl;
  66. for (int i = 0; i < N; i++)
  67. {
  68. for (int j = 0; j < P; j++)
  69. cout << a[i][j] << " ";
  70. cout << endl;
  71. }
  72. }
  73. int main()
  74. {
  75. int a[N][P];
  76. setlocale(LC_ALL, "Rus");
  77. srand(time(NULL));
  78. cout << "Сортировать строк по убыванию их сумм по строкам" << endl;
  79. cinn(a, N, P);
  80. coutt(a, N, P);
  81. sum_row(a, N, P);
  82. sort(a, N, P);
  83. rez(a, N, P);
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement