Sum of all submatrix from a given matrix

I tried writing the code to find Sum of all submatrix from a given matrix using prefix sum array method,however my output did not come out to be as expected. Please help me find the error.

Link to my code: https://ide.codingblocks.com/s/266788

hello @gptradhika1208


for ti=0 or tj=0

ti-1 or tj-1 can become negative ,
so use if statements to handle these corner cases seperately

Yes, I added this statement :

I added the following statement after the for loop: if(ti == 0 || tj == 0) { sum += psa[ti][tj];} else{ sum += psa[bi][bj] - psa[ti-1][bj] - psa[bi][tj-1] + psa[ti-1][tj-1];}

But it still comes out to be incorrect.

no not like that,

split it into these four cases
ti==0 && tj==0

ti==0 && tj!=0

ti!=0 && tj==0

ti!=0 && tj!=0

I was still not able to resolve the issue. Please check my code: https://ide.codingblocks.com/s/268955

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.