Implement a main function for the following function, it should return in a sing
ID: 3723150 • Letter: I
Question
Implement a main function for the following function, it should return in a single object the value of the maximum subsequence and the indices of the actual sequence, in the main function there I need to change the lenght of the array(something like n=a number(lenght of the array) and it needs to have ramdom positive and negative integers, if you need to modify the function go ahead I just need it working because I will test it with arrays of lenghts of 1,000, 10,000 and 100,000
Code:
int* maxSubSum4(const vector<int> & a)
{
int maxSum =0, thisSum= 0;
int from, till;
int n = a.size()+1;
int result[n];
int *p = result;
for(int j=0;j<a.size();j++)
{
this Sum += a[j];
if(thisSum>maxSum)
{
maxSum=thisSum;
from =i;
till =j;
}
else if(thisSum<0)
thisSum = 0;
}
result[0] = maxSum;
int ind=1;
for(int i=from; i<=till;i++)
{
result[ind] = i;
ind++;
}
return p;
}
Explanation / Answer
#include #include #include #include using namespace std; int* maxSubSum4(const vector& a) { int maxSum = 0, thisSum = 0; int from, till; int n = a.size() + 1; int result[n]; int* p = result; for (int j = 0; j maxSum) { maxSum = thisSum; from = i; till = j; } else if (thisSum < 0) thisSum = 0; } result[0] = maxSum; int ind = 1; for (int i = from; i size; vector vec; int range = size/10; for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.