Why is the code after my for loop being ignored?

I don’t think you’ll need to know the context of the problem to answer this question, but I’ll give it just in case.

-In the past N weeks, we’ve measured the amount of rainfall every day, and noted it down for each day of the week. Return the number of the first week of the two week period where there were the most days without rain.

The code gives no warnings or errors, and if I try to print dryestweeks inside the second for loop, then it returns the correct answer. However, all of the code after the second for loop seems to be getting ignored, and I’m getting Process returned -1073741819 (0xC0000005). The issue has to lie in the 2nd for loop, because if I comment it out then both “test2” and dryestweeks get printed, and the program returns 0.

#include <iostream>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main() {
    int weeks;
    cin >> weeks;
    vector<int> v[weeks];

    for (int i = 0;i < weeks; i++) {
        int a, b, c, d, e, f, g;
        cin >> a >> b >> c >> d >> e >> f >> g;
        v[i].push_back(a);
        v[i].push_back(b);
        v[i].push_back(c);
        v[i].push_back(d);
        v[i].push_back(e);
        v[i].push_back(f);
        v[i].push_back(g);
    }

    int mostdrydays = 0;
    int dryestweeks = 0;

    for (int i = 0; i < weeks; i++) {
        int weeklydrydays = count(v[i].begin(), v[i].end(), 0);
        int nextweekdrydays = count(v[i+1].begin(), v[i+1].end(), 0);
        int biweeklydrydays=weeklydrydays+nextweekdrydays;

        if (biweeklydrydays > mostdrydays) {
            mostdrydays = biweeklydrydays;
            dryestweeks = i + 1;
        }
    }

    cout << "test2" << endl;
    cout << dryestweeks << endl;

    return 0;
}

An example of an input would be:

6
5 10 15 20 25 30 35
0 2 0 0 0 0 0
0 0 0 1 0 3 0
0 1 2 3 4 5 6
5 1 0 0 2 1 0
0 0 0 0 0 0 0

The program should print “2” with the above input.

Answer

The second loop has an overflow.

You first defined v[weeks] and then the second loop goes from [0, weeks[ but you are retrieving the next week with v[i + 1]. I don’t know exactly what are you are trying to achieve, but if you do

for(int i = 0; i < weeks - 1; i++) 
{
    ...
}

it executes properly.