Hey everyone, I have this problem:
I should divide a 2D array into square regions with a given parameter(d), find the maxes of each region and then their sum.
All I have done up now is this code here:
#include <iostream>
usingnamespace std;
int main()
{
staticint array1[200][200];
int i, j, m, n,d;
cin>>m>>n>>d;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
cin>>array1[i][j];
}
}
for (i = 0; i < d; ++i)
{
for (j = 0; j < d; ++j)
{
cout<<array1[i][j];
}
cout<<"\n";
}
for (i = d; i < m; ++i)
{
for (j = 0; j < d; ++j)
{
cout<<array1[i][j];
}
cout<<"\n";
}
for (i = 0; i < d; ++i)
{
for (j = d; j < n; ++j)
{
cout<<array1[i][j];
}
cout<<"\n";
}
for (i = d; i < m; ++i)
{
for (j = d; j < n; ++j)
{
cout<<array1[i][j];
}
cout<<"\n";
}
}
But the problem is that this solution only works with this input:
5 6 3
0 8 1 1 10 6
6 8 7 0 3 9
0 7 6 8 6 5
4 0 2 7 2 0
4 4 5 7 5 1
Because lets say if there are 11 columns, it takes the first three and then it takes up until the end of the columns and it doesn't divide it three by three(where three is the parameter the user enters)
Can you please help me out and give me a general solution for this code?
Thank youuuuu
Thanks for the reply but actually I couldnt quite understand what you meant..
To be more precise, starting from the first loop of dividing it in regions, it prints these regions:
0 8 1
6 8 7
4 0 2
4 4 5
1 10 6
0 3 9
8 6 5
7 2 0
7 5 1
and if the matrix would be lets say 6x11, the rows would continue up to 11 and not divide it by 3..i hope you understand what i am saying
int vstride = rows / d;
int hstride = cols / d;
for ( int row = 0; row < rows; row += vstride ) {
for ( int col = 0; col < cols; col += hstride ) {
// print region [row..row+vstride)[col..col+hstride)
}
}