-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixRegionSum.cpp
80 lines (68 loc) · 1.33 KB
/
MatrixRegionSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
using namespace std;
int main()
{
int m,n;
int ** mat;
//top-left, bottom-right variable
int t,l,b,r;
cout<<"Enter m,n";
cin>>m>>n;
cout<<"M is:"<<m<<" N is: "<<n<<endl;
//Create matrix
//*mat=(int *)malloc(m*sizeof(int));
//Alloc m pointers
mat=new int* [m];
//Alloc memory for individual rows
for(int i=0;i<m;i++)
{
mat[i]=new int [n];
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<"Enter element("<<i<<","<<j<<"):";
cin>>mat[i][j];
}
}
//Precalculation step
int ** sum_matrix=new int* [m];
for(int i=0;i<m;i++)
{
sum_matrix[i]=new int [n];
}
//top row sum
sum_matrix[0][0]=mat[0][0];
for(int i=1;i<n;i++)
{
sum_matrix[0][i]=sum_matrix[0][i-1]+mat[0][i];
}
//left col sum
sum_matrix[0][0]=mat[0][0];
for(int i=1;i<m;i++)
{
sum_matrix[i][0]=sum_matrix[i-1][0]+mat[i][0];
}
//other regions
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
sum_matrix[i][j]=mat[i][j]+sum_matrix[i-1][j]+sum_matrix[i][j-1]-sum_matrix[i-1][j-1];
}
}
//printing sum_matrix
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<sum_matrix[i][j]<<" ";
}
}
//Get the region now :)
cout<<"Enter the region top,left,bottom,right edge";
cin>>t>>l>>b>>r;
int sum = sum_matrix[b][r]+sum_matrix[t][l]-sum_matrix[b][l]-sum_matrix[t][r];
cout<<"Sum is:"<<sum<<endl;
}