0

This is the question that I was trying to solve on HackerRank: https://www.hackerrank.com/challenges/2d-array/problem

Just a VERY rough mapping of what I was going to make

Here is my function body:

int hourglassSum(vector<vector<int>> arr) {
    vector <int> sum;

    for(int i=0, s=0, countI=0;countI<4&&s<16;i++){  //Sums hourglass values one column  
        for(int j=0,countJ=0;countJ<4;j++){          //at a time at each row into vector sum
            sum[s]+=arr[0+i][0+j];
            sum[s]+=arr[0+i][1+j];
            sum[s]+=arr[0+i][2+j];
            sum[s]+=arr[1+i][1+j];
            sum[s]+=arr[2+i][0+j];
            sum[s]+=arr[2+i][1+j];
            sum[s]+=arr[2+i][2+j];
            countJ++;
            s++;
        }
        countI++;
    }
    sort (sum.begin(), sum.end());

    return sum.back();
}

Line by line verification of assigments in loops:

i=0 | j=0 | s=0 | countI=0 | countJ=0 | sum[0]=1+1+1+1+1+1+1=7
i=0 | j=1 | s=1 | countI=0 | countJ=1 | sum[1]=1+1+0+0+1+1+0=4
i=0 | j=2 | s=2 | countI=0 | countJ=2 | sum[2]=1+0+0+0+1+0+0=2
i=0 | j=3 | s=3 | countI=0 | countJ=3 | sum[3]=0+0+0+0+0+0+0=0
--------------------------------------------------------------
i=1 | j=0 | s=4 | countI=1 | countJ=0 | sum[4]=0+1+0+1+0+0+2=4
i=1 | j=1 | s=5 | countI=1 | countJ=1 | sum[5]=1+0+0+1+0+2+4=8
i=1 | j=2 | s=6 | countI=1 | countJ=2 | sum[6]=0+0+0+0+2+4+4=10
i=1 | j=3 | s=7 | countI=1 | countJ=3 | sum[7]=0+0+0+0+4+4+0=8
--------------------------------------------------------------
i=2 | j=0 | s=8 | countI=2 | countJ=0 | sum[8]=1+1+1+0+0+0+0=3
i=2 | j=1 | s=9 | countI=2 | countJ=1 | sum[9]=1+1+0+2+0+0+2=4
i=2 | j=2 | s=10| countI=2 | countJ=2 | sum[10]=1+0+0+4+0+2+0=7
i=2 | j=3 | s=11| countI=2 | countJ=3 | sum[11]=0+0+0+4+2+0+0=6
--------------------------------------------------------------
i=3 | j=0 | s=12| countI=3 | countJ=0 | sum[12]=0+0+2+0+0+0+1=3
i=3 | j=1 | s=13| countI=3 | countJ=1 | sum[13]=0+2+4+0+0+1+2=9
i=3 | j=2 | s=14| countI=3 | countJ=2 | sum[14]=2+4+4+2+1+2+4=19
i=3 | j=3 | s=15| countI=3 | countJ=3 | sum[15]=4+4+0+0+2+4+0=14
--------------------------------------------------------------

After sort (sum.begin(), sum.end());

sum[0] = 0
sum[1] = 2
sum[2] = 3
sum[3] = 3
sum[4] = 4
sum[5] = 4
sum[6] = 4
sum[7] = 6
sum[8] = 7
sum[9] = 7
sum[10] = 8
sum[11] = 8
sum[12] = 9
sum[13] = 10
sum[14] = 14
sum[15] = 19

return sum.back(); should return 19? but instead I get segmentation fault error.

The error code below:

 Reading symbols from Solution...done.
[New LWP 128917]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400ea4 in hourglassSum (arr=...) at Solution.cpp:17
17              sum[s]+=arr[2+i][2+j];
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"
bikboy
  • 13
  • 1

2 Answers2

0

initial value vector of type int is '0'. you are ok, with this concept. but you missed declaring the size of vector (initial).

here is the standard syntax of declaring vector:

vector<type> variable_name (number_of_elements);

so you need to modify the line where you are declaring vector with this :

vector <int> sum(N); // N is the size of vector to be create
Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17
0

This error occurs because of an out-of-scope array index that is causing a buffer overflow, an incorrectly initialised pointer, etc.

A signal is generated when a program either tries to read or write outside the memory that is allocated for it or to write memory that can only be read.

For example, you are accessing a[-1] in a language that does not support negative indices for an array.

Errors made in the code:

  1. You are trying to write on the memory locations(vector sum) which are not initialised. So initialise the vector with a constant size of 16.
  2. Also, no conditions were added for the maximum allowed values indices i and j in the 2 for loops.

Have a look at the following code which has accepted status on Hackerrank:

#include <bits/stdc++.h>

using namespace std;

// Complete the hourglassSum function below.
int hourglassSum(vector<vector<int>> arr) {
    vector <int> sum(16);

    for(int i=0, s=0, countI=0;countI<4&&s<16;i++){  //Sums hourglass values one column  
        for(int j=0,countJ=0;countJ<4;j++){          //at a time at each row into vector sum
            if(i+2<6 && j+2<6){
                sum[s]+=arr[0+i][0+j];
                sum[s]+=arr[0+i][1+j];
                sum[s]+=arr[0+i][2+j];
                sum[s]+=arr[1+i][1+j];
                sum[s]+=arr[2+i][0+j];
                sum[s]+=arr[2+i][1+j];
                sum[s]+=arr[2+i][2+j];
                countJ++;
                s++;
            }
        }
        countI++;
    }
    sort (sum.begin(), sum.end());

    return sum.back();
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35
  • 2
    I wouldn't suggest using [`#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – JeJo Nov 18 '19 at 07:53
  • Thank you @Deepak. I think you could do without the limits for i and j because countI and countJ already check the number of checks per row/column. After each row is done, I gets reinitialized to 0, thus not passing the limit. Once all columns are done it exits. – bikboy Nov 18 '19 at 16:48