1

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.

Chris
  • 26,361
  • 5
  • 21
  • 42
33lives
  • 75
  • 5
  • As is typical for examples on so-called "competition" or "online judge" sites, the code is actually *invalid*, and contains many bad habits. If you're learning programming and C++ then get [some good books](https://stackoverflow.com/a/388282/440558) and take classes. And stay away from such sites, they only cause harm. – Some programmer dude Oct 31 '21 at 18:11
  • 7
    You solve problems like these in a few keystrokes. [-fsanitize=address](https://godbolt.org/z/P35Wf59ba). Bam! main(), line 26. What do we have on line 26? `v[i+1]`. What is the range of `i`? What is the range of indices of `v`? Good bye! – n. m. could be an AI Oct 31 '21 at 18:13
  • As for the reason behind the *crash* you probably have, it's because you use indexes that are out of bounds and that leads to *undefined behavior*. – Some programmer dude Oct 31 '21 at 18:13
  • Your code reads past the end of the array at the last iteration (also uses a VLA of vector, which isn't proper C++). Also, never directly include `bits/*` – Hasturkun Oct 31 '21 at 18:13

2 Answers2

2

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.

Philippe Miron
  • 178
  • 1
  • 9
0

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.

codesnerd
  • 767
  • 2
  • 8
  • 23
Prakhar Tomar
  • 23
  • 1
  • 5