1

What is wrong with this code?

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a=0;int b=0;
    vector<vector<int>> arr(6);
    for (int i = 0; i < 6 ; ++i) {
        arr[i].resize(6);

        for (int j = 0; j < 6; ++j) {
            cin >> arr[i][j];
        }

        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            a=arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2] 
[j+2];
        }
        if(a>b){
            b=a;
            a=0;
        }
        else{
            a=0;
        }
    }
    cout<<b;


    return 0;
}

The left bottom most hourglass is not considered in this code, it is working well in all the other parts. can you tell me the mistake here?

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 4
    Please explain what the code is supposed to do, include input and expected output. Then explain why the code is not doing what you expect, maybe by explaining how the actual output does not match the expected one. What does that mean: "the left bottom most hour glass is not considered" ? – 463035818_is_not_an_ai Feb 10 '20 at 09:58
  • 2
    Unrelated to your problem but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Feb 10 '20 at 09:58
  • 1
    Also please take some time to read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Feb 10 '20 at 09:59

1 Answers1

0

the left bottom most hourglass is not considered in this code, it is working well in all the other parts.

No, not only the left bottom most hourglass is not considered, but rather all hourglasses except the last from each row aren't. That's because the hourglass sum a is only compared to the current maximum b after the loop for the columns in a row, so only the last sum is considered. To correct this, you can change

        {
            a=arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2] 
[j+2];
        }
        if(a>b){
            b=a;
            a=0;
        }
        else{
            a=0;
        }

to

        {
            a=arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2];
            if (a>b) b=a;
        }
Armali
  • 18,255
  • 14
  • 57
  • 171