Polnochniy

Untitled

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