Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <chrono>
  4. using namespace std;
  5. int* func(int** mas, int* N, int* M, int* sum, int* a, int* b, int* c, int* d) {
  6.  
  7. for (int i = 0; i < *N; i++)
  8. {
  9. int sum = 0;
  10. for (int j = 0; j < *M; j++)
  11. {
  12. sum += mas[i][j];
  13. }
  14. if (*a < sum)
  15. {
  16. *a = sum;
  17. *b = i;
  18. }
  19. cout << endl;
  20. if (*c > sum)
  21. {
  22. *c = sum;
  23. *d = i;
  24. }
  25. cout << endl;
  26.  
  27. cout << "Result " << i + 1 << " is " << sum;
  28.  
  29. }
  30. return a,b,c,d;
  31. }
  32. int** swapping(int** mas, int* N, int* M, int* b, int* d) {
  33. for (int i = 0; i < *N; i++)
  34. {
  35. for (int j = 0; j < *M; j++) {
  36. swap(mas[*b][j], mas[*d][j]);
  37. }
  38. }
  39. return mas;
  40.  
  41. }
  42. void out(int** mas, int* N, int* M) {
  43. for (int i = 0; i < *N; i++) {
  44. cout << " ";
  45. cout << endl;
  46. for (int j = 0; j < *M; j++)
  47. {
  48. cout << mas[i][j] << " ";
  49. }
  50. cout << " ";
  51. }
  52.  
  53. }
  54.  
  55. int** generate(int** mas, int* N, int* M) {
  56. for (int i = 0; i < *N; i++) {
  57. for (int j = 0; j < *M; j++) {
  58.  
  59. mas[i][j] = rand() % 20 + (-9);
  60. }
  61.  
  62. }
  63. return mas;
  64.  
  65. }
  66.  
  67. int main()
  68. {
  69.  
  70. srand(time(NULL));
  71. int M;
  72. int N;
  73. int a = 0, b = 0, c = 0, d = 0;
  74.  
  75.  
  76. cout << "Input matrix NxM: ";
  77. cin >> N;
  78. cin >> M;
  79. auto start = chrono::high_resolution_clock::now();
  80.  
  81. int* sum = new int[N];
  82.  
  83. int** mas = new int* [N];
  84. for (int i = 0; i < N; i++) {
  85. mas[i] = new int[M];
  86. }
  87.  
  88.  
  89.  
  90. generate(mas, &N, &M);
  91. out(mas, &N, &M);
  92. cout << endl;
  93. func(mas, &N,&M, sum, &a, &b, &c, &d);
  94. cout <<endl<<endl<<"Max sum is: "<< a<<endl <<"Row of max sum: "<< b+1<<endl<<"Min sum is: " << c <<endl<<"Row of min sum is: "<< d+1;
  95. cout << endl;
  96. swapping(mas, &N, &M, &b, &d);
  97. out(mas, &N, &M);
  98. for (int i = 0; i < M; i++) {
  99. delete [] mas[i];
  100. }
  101. delete [] mas;
  102. auto end = chrono::high_resolution_clock::now();
  103.  
  104. chrono::duration<float> duration = end - start;
  105. cout << endl<<endl;
  106. cout << "Time is " << duration.count() << endl << endl << endl << endl;
  107.  
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement