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"