Advertisement
Polnochniy

Untitled

Jan 15th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 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.  
  17. int main()
  18. {
  19. int a[N][P];
  20. setlocale(LC_ALL, "Rus");
  21. srand(time(NULL));
  22. cout << "Сортировать строк по убыванию их сумм по строкам" << endl;
  23. cout << "Введите элементы массива" << endl;
  24. for (int i = 0; i < N; i++)
  25. {
  26. for (int j = 0; j < P; j++)
  27. {
  28. cin >> a[i][j];
  29. }
  30. cout << endl;
  31. }
  32. cout << "Вывод элементов " << endl;
  33. for (int i = 0; i < N; i++)
  34. {
  35. for (int j = 0; j < P; j++)
  36. {
  37. cout << a[i][j] << " ";
  38. }
  39. cout << endl;
  40. }
  41. cout << endl;
  42. cout << "Сумма элементов по строкам " << endl;
  43. for (int k = 0; k < N; k++)
  44. {
  45. cout << sum(a[k],P) << " " << endl;
  46. }
  47. for (int i = 0; i < N - 1; i++)
  48. {
  49. for (int j = i + 1; j < N; j++)
  50. {
  51. if (sum(a[i], P) < sum(a[j], P)) // вот тут .
  52. {
  53. swap(a[i], a[j]);
  54. }
  55. }
  56. }
  57. cout << "Результат " << endl;
  58. for (int i = 0; i < N; i++)
  59. {
  60. for (int j = 0; j < P; j++)
  61. cout << a[i][j] << " ";
  62. cout << endl;
  63. }
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement