SUM OF ALL SUBMATRICES APPROACH 2

#include
//refer notebooks for better notes.
using namespace std;

int main()
{
int row,col;
cout<<“ENTER ROWS AND COLUMNS\n”;
cin>>row>>col;
int arr[100][100];

//
cout<<"ENTER THE ELEMENTS OF THE ARRAY\n";
for(int i=0;i<row;i++)
{
	for(int j=0;j<col;j++)
	{
		cin>>arr[i][j];
	}
}

//now converting this array to prefix sum array;

int prev=0;
for(int i=0;i<row;i++)
{
	prev=0;
	for(int j=0;j<col;j++)
	{
		arr[i][j]=arr[i][j]+prev;
		prev=arr[i][j];
	}
}

for(int i=0;i<row;i++)
{
	prev=0;
	for(int j=0;j<col;j++)
	{
		arr[j][i]=arr[j][i]+prev;
		prev=arr[j][i];
	}
}
//Now our prefix sum array is completed.

//finding sum of all the subarrays-relate to approach 1.

int sum=0,P,Q,M,N;

for(int li=0;li<row;li++)
{
	for(int lj=0;lj<col;lj++)
	{
		for(int bi=li;bi<row;bi++)
		{
			for(int bj=lj;bj<col;bj++)
			{

			
				P=arr[bi][bj];
				Q=arr[li-1][bj];
				M=arr[bi][lj-1];
				N=arr[li-1][lj-1];
				sum=sum+(P-Q-M+N);



			}
		}
	}
}

cout<<"THE SUM OF ALL THE SUBARRAYS OF THE MATRIX IS\n"<<sum;

}

ACCORDING TO WHAT SIR HAS EXPLAINED IN THE TUTORIAL, I’d implemmented the code, but the output is wrong?

So, please check my code.

Please save your code on ide.codingblocks.com and then share its link.

This is the link of the code, please check.

Refer this https://ide.codingblocks.com/s/290506

Add extra conditions for li and lj also, when they are 0. In that case there may be a chance of getting negative index

Yes , I’d put extra conditions for li and lj, but that is also not working.

So, what to do now? ? ? . . .

You did not impose the conditions properly.
Check your code now

Thank you so much pratyush brother.