Polnochniy

Untitled

Jan 14th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 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. a[i][j] = rand() % 109 + (-37);
  29. cout << a[i][j] << " ";
  30. }
  31. cout << endl;
  32. }
  33. cout << endl;
  34. for (int i = 0; i < N-1; i++)
  35. {
  36. for (int j =i+1; j<N ; j++)
  37. {
  38. if (sum(a[i],N) < sum(a[j],P)) // вот тут .
  39. {
  40. swap(a[i], a[j]);
  41. }
  42. }
  43. }
  44. cout << "Результат " << endl;
  45. for (int i = 0; i < N; i++)
  46. {
  47. for (int j = 0; j < P; j++)
  48. cout << a[i][j] << " ";
  49. cout << endl;
  50. }
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment