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.
CodePudding user response:
The second loop has an overflow.
You first defined v[week] and then the second loop goes from [0, week[ 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.
CodePudding user response:
For the given example of input, in the last iteration (i = 5
) of the second loop, index i 1
(=6) will be out of the bound for v[i 1]
(legal indices for v
will be from 0 to 5).
The second loop is iterating one more time than required.