Answer is not as expected

int submatrix_2(int a[][10],int n)
{
int sum=0;
int prefix_array[n][n];
for(int i=0;i<n;i++)
prefix_array[0][i]=a[0][i];
for(int i=1;i<n;i++)
for(int j=0;j<n;j++)
prefix_array[i][j]=a[i][j]+a[i-1][j];
for(int j=1;j<n;j++)
for(int i=0;i<n;i++)
{ prefix_array[i][j]=prefix_array[i][j]+prefix_array[i][j-1];}

for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
		cout<<prefix_array[i][j]<<" ";


for(int  TLi=0;TLi<n;TLi++)
	for(int TLj=0;TLj<n;TLj++)
		for(int BRi=TLi;BRi<n;BRi++)
			for(int BRj=TLj;BRj<n;BRj++)
				{cout<<prefix_array[BRi][BRj]<<" "<<prefix_array[BRi][TLj-1]<<" "<<prefix_array[TLi-1][BRj]<<" "<<prefix_array[TLi-1][TLj-1]<<endl;
				sum+=(prefix_array[BRi][BRj]-prefix_array[BRi][TLj-1]-prefix_array[TLi-1][BRj]+prefix_array[TLi-1][TLj-1]);}
return sum;

}

Hello @Nidhi_Alipuria,

Please, share your code on Online Coding Blocks.
The way you have shared may introduced many syntax errors to it.
Steps:

  1. Paste your code at https://ide.codingblocks.com/.
  2. Save it there.
  3. Share the URL generated.

prefix_array[BRi][TLj-1], prefix_array[TLi-1][BRj], prefix_array[TLi-1][TLj-1] will cause issue for the case when either TLj, TLi or both are 0.

Correct your code for this.

Hope, this would help.
Give a like if you are satisfied.