Maximum Subarray sum

#include
using namespace std;
int msum(int a[], int n)
{
int max_ending_here=0, max_so_far=0, i;
for(i=0;i<n;i++)
{
max_ending_here=max_ending_here+a[i];
if(max_ending_here<0)
max_ending_here=0;
if(max_so_far<max_ending_here)
max_so_far=max_ending_here;
}
return max_so_far;
}
int main()
{
int t, n, a[10], i;
cin>>t;
while(t–)
{
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
cout<<msum(a, n)<<endl;
}
return 0;
}
Why is this code showing run-time error?

Hi!
Try using a bigger array, you are using array of just 10 elements.

Thank you sir, now the error is resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.