something not working help

#include <iostream> #include<time.h> using namespace std; void main() { setlocale(LC_ALL, "ukr"); srand(time(NULL)); const int n = 15; bool change = true; int arr[n], m = 0,p=0; for (int i = 0; i < n; i++) { arr[i] = rand() % 10 - 5; cout << arr[i] << " "; } cout << endl; for (int i = 0; i < n; i++) { if (arr[i] >= 0) { m += arr[i]; } else { p = m; m = 0; } } if (p > m) cout << endl << p << endl; else cout << endl << m << endl; } 
  • 2
    and in fact an error in the else { p = m; m = 0; } block else { p = m; m = 0; } else { p = m; m = 0; } else { p = m; m = 0; } here it is necessary to check what is more and only then assign. - pavel
  • @pavel yes, that's right, now it works) - tkachuk
  • @VladimirGamalian don't quite understand what this is about) - tkachuk
  • @pavel, make a comment in the form of a response - Grundy

1 answer 1

Your error in the unconditional assignment in the block:

 else { p = m; m = 0; } 

Here we must check that p less than m . For example:

 else { if (p < m) p = m; m = 0; }