Advertisement
annie_02

zad2.0

Sep 1st, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. /**
  2. *
  3. * Solution to homework assignment S
  4. * Introduction to programming course
  5. * Faculty of Mathematics and Informatics of Sofia University
  6. * Winter semester 2020/2021
  7. *
  8. * @author Anelia Keranova
  9. * @idnumber 7MI0600055
  10. * @task 2
  11. * @compiler VC
  12. *
  13. */
  14.  
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. int sum(int* arr, int s, int j) {
  19. int res = 0;
  20. for (int i = s; i <= j; i++) {
  21. res = res + arr[i];
  22. }
  23. return res;
  24. }
  25. int subArray(int* arr, int size) {
  26. int max = arr[0];
  27. for (int i = 0; i < size; i++) {
  28. for (int j = size; j >= i; j--) {
  29. int temp = sum(arr, i, j);
  30. if (max < temp) {
  31. max = temp;
  32. }
  33. }
  34. }
  35. return max;
  36. }
  37. int main()
  38. {
  39. int arr[8] = { -2, -3, 4, -1, -2, 1, 5, -3 };
  40. cout << subArray(arr, 8);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement