Please check this code it is about finding sum of all sub matrices from a matrix O(4)

#include
using namespace std;
int main()
{cout<<“enter the array”<<endl;
int arr[2][2],i=0,j=0;
for( i=0;i<2;i++)
{for( j=0;j<2;j++)
{cin>>arr[i][j];
}
}
int csum[2][2];
csum[0][0]=arr[0][0];
//creating prefix sum matrix(csum) , adding elements row wise
for(int l=0;l<2;l++)
{csum[l][0]=arr[l][0];
for(int k=1;k<2;k++)
{csum[l][k]=csum[l][k-1]+arr[l][k];
}
}
//creating prefix matrix adding collumn wise
for(int m=0;m<2;m++)
{for(int n=1;n<2;n++)
{csum[n][m]+=csum[n-1][m];
}
}
//printing prefix sum to check wether it is correct or not
for( i=0;i<2;i++)
{for( j=0;j<2;j++)
{cout<<csum[i][j]<<" ";
}
cout<<endl;
}
//adding sum with algorithm taught in lecture
int sum=0;
for(i=0;i<2;i++)
{for(j=0;j<2;j++)
{for(int m=i;m<2;m++)
{for(int n=j;n<2;n++)
{sum+=csum[m][n]-csum[i-1][n]-csum[m][j-1];
}
}
}
}
//printing out the sum
cout<<sum<<endl;

}

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

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.