Advertisement
Guest User

prawidlowy

a guest
Feb 26th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <climits>
  5.  
  6. using namespace std;
  7.  
  8. int **createArray(int, int);
  9. void maxSubArraySum();
  10. void deleteArray(int, int);
  11.  
  12. int main()
  13. {
  14.  
  15. maxSubArraySum();
  16.  
  17. return 0;
  18. }
  19.  
  20. int kadane(int *subArray, int top, int bottom, int n)
  21. {
  22.  
  23. int sum = 0;
  24. int maxSum = INT_MIN;
  25. bottom = -100;
  26.  
  27. for (int i = 0; i < n; ++i)
  28. {
  29. sum += subArray[i];
  30. if (sum < 0)
  31. sum = 0;
  32.  
  33. else if (sum > maxSum)
  34. {
  35. maxSum = sum;
  36. bottom = i;
  37. }
  38. }
  39.  
  40. if (bottom >= 0) // to check if (sum>maxSum) condition is met
  41. return maxSum;
  42.  
  43. maxSum = subArray[0]; // to check whether array contains only negative numbers
  44.  
  45. for (int j = 0; j < n; ++j)
  46. {
  47. if (subArray[j] > maxSum)
  48. maxSum = subArray[j];
  49. else
  50. maxSum = 0;
  51. }
  52. return maxSum;
  53. }
  54.  
  55. void maxSubArraySum()
  56. {
  57.  
  58. int z, n, m;
  59. int top, right, bottom, left, sum;
  60. int finalTop, finalRight, finalBottom, finalLeft, maxSum = INT_MIN; // final output
  61.  
  62. std::cout << "Enter number of sets:" << std::endl;
  63. std::cin >> z;
  64.  
  65. for (int i = 0; i < z; ++i)
  66. {
  67. cout << "Enter columns and rows" << endl;
  68. cin >> n >> m;
  69.  
  70. int **t = createArray(n, m);
  71.  
  72. for (left = 0; left < m; ++left)
  73. {
  74. int subArray[n] = {0};
  75.  
  76. for (right = left; right < m; ++right)
  77. {
  78. for (int j = 0; j < n; ++j) // extracting single columns from t array
  79. subArray[j] += t[j][right];
  80.  
  81. sum = kadane(subArray, top, bottom, n);
  82.  
  83. if (sum > maxSum)
  84. maxSum = sum;
  85. }
  86. }
  87. cout << "Max sum is: " << maxSum << endl;
  88. }
  89. }
  90.  
  91. int **createArray(int n, int m)
  92. {
  93.  
  94. int **tab = nullptr;
  95. if ((n >= 1 && n <= 100) && (m >= 1 && m <= 100))
  96. {
  97. try
  98. {
  99. tab = new int *[n];
  100. }
  101.  
  102. catch (std::bad_alloc &b)
  103. {
  104. std::cout << "bad alloc: " << b.what() << std::endl;
  105. }
  106. for (int i = 0; i < n; ++i)
  107. try
  108. {
  109. tab[i] = new int[m];
  110. }
  111. catch (std::bad_alloc &b)
  112. {
  113. std::cout << "bad alloc: " << b.what() << std::endl;
  114. }
  115.  
  116. std::cout << "Enter array elements: " << std::endl;
  117. for (int i = 0; i < n; ++i)
  118. for (int j = 0; j < m; ++j)
  119. std::cin >> tab[i][j];
  120.  
  121. return tab;
  122. }
  123. else
  124. std::cout << "tu bedzie zakonczenie programu" << std::endl;
  125. }
  126.  
  127. void f_deleteArray(int **t, int m)
  128. {
  129. for (int i = 0; i < m; ++i)
  130. delete[] t[i];
  131. delete[] t;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement