1

Given an array, I am trying to find the maximum sub-array sum. A sub-array is as follows. For example, I get the following array: [9, -7, 5, 3, 91]. Whilst [9, -7, 5] is a sub-array, [9, 5, 3, 91] is not. My code is below:

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int arraylen, subarraylen, subarraysum, itervar1, itervar2, itervar3, incrementvar;
    cin >> arraylen;
    vector<int> mainarray(arraylen);
    vector<int> sumarray(arraylen * (arraylen-1) + 1);
    for (itervar1 = 0; itervar1 < arraylen; itervar1++) {
        cin >> mainarray[itervar1];
    }
    sumarray[0] = 0;
    for (itervar1 = 0; itervar1 < arraylen; itervar1++) {
        for (itervar2 = arraylen; itervar2 > 0; itervar2--) {
            subarraylen = itervar2-itervar1;
            if (subarraylen < 1) {
                continue;
            }
            vector<int> subarray(subarraylen);
            incrementvar = 0;
            for (itervar3 = itervar1; itervar3 < itervar2; itervar3++) {
                subarray[incrementvar] = mainarray[itervar3];
                incrementvar++;
            }
            subarraysum = 0;
            for (itervar3 = 0; itervar3 < subarraylen; itervar3++) {
                subarraysum += subarray[itervar3];
            }
        }
    }
    return 0;
}

For some reason, it doesn't work; just infinitely loops around. Any help would be appreciated.

5
  • 1
    Why isn't it a sub array? the numbers have to be consecutive? Commented Oct 25, 2017 at 0:55
  • 4
    I recommend you take some time to read How to debug small programs by Eric Lippert, and learn how to use a debugger to step through your code line by line. Commented Oct 25, 2017 at 0:55
  • @Jeff Yes, the numbers have to be consecutive in the array. Commented Oct 25, 2017 at 1:18
  • 1
    Just to be clear, is this a homework assignment? Commented Oct 25, 2017 at 13:57
  • @Jeff Yes it is. Commented Oct 26, 2017 at 1:05

1 Answer 1

2

First, here is a routine to list all the sub arrays:

vector<int> vec{ 9, -7, 5, 3, 91 };
int sz = vec.size();
for(int start = 0; start < sz; start++)
{
    for(int end = start; end < sz; end++)
    {
        for(int j = start; j <= end; j++)
        {
            cout << vec[j] << ", ";
        }
        cout << "\n";
    }
}

Now you just have to get the total in loop j, add to a vector sum. You don't need to know the size of sum before hand. Use push_back to add total to sum.

Note, the array itself is included as a sub array, you can exclude that array depending on what the definition is.

Sign up to request clarification or add additional context in comments.

3 Comments

When I run this routine, I get an error for your vec{9, -7, 5, 3, 91} assignment line. How can I fix this?
I used C++ 11 standard to initialize a vector. See here: ideone.com/mNDK0k You have an older compiler, keep using your own method to initialize the vector.
Thanks for the clarification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.