The sum of the submatrix

Mar 1, 2021 at 9:30am
From the first line, read the numbers N and M from the keyboard. From the following N lines, read M natural numbers that represent the intensity of each pixel.
The next line will read the number G, representing the number of areas in the image for which you need to find out the total intensity.

On each of them G lines will be 4 numbers i1, j1, i2, j2, representing the coordinates of the submatrix.

The program will display on the screen the sum of the elements in the submatrix

Input:
4 5
-1 -1 -4 -4 -1
-1 -1 -5 -5 -1
-1 -1 -1 -9 -1
-1 -6 -7 -8 -1
3
1 1 4 5
3 4 3 4
2 2 3 4
Output:
-60
-9
-22


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
using namespace std;

int main() {
    int N, M, G, mt[101][101], submt[101][101];
    cin >> N >> M;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            cin >> mt[i][j];
        }
    }
    cin >> G;
    for (int i = 1; i<= G; i++) {
        for (int j = 1; j <=4; j++) {
            cin >> submt[i][j];
        }
    }
    return 0;
}
Mar 1, 2021 at 10:04am
What is the question?
Mar 1, 2021 at 11:15am
How to display the sum of the elements in the submatrix?
Mar 1, 2021 at 11:34am
2 2 3 4

Does that mean "rows 2-3, columns 2-4"?

When you did read values to mt, you did loop rows 1-N, columns 1-M.
In reality your array mt has rows 0-100, columns 0-100.
In other words: you have shown that you can loop a submatrix.

Have you not had "sum of array" before? Submatrix is an array.
Mar 1, 2021 at 12:39pm
Hello swingby,

This will give you an idea of your program:
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
#include <iostream>
#include <sstream>

using namespace std;  // <--- Best not to use.

int main()
{
    constexpr int MAXRWOS{ 10 };
    constexpr int MAXCOLS{ 10 };

    std::istringstream mxSS{ "-1 -1 -4 -4 -1 -1 -1 -5 -5 -1 -1 -1 -1 -9 -1 -1 -6 -7 -8 -1" };
    std::istringstream subMxSS{ "1 1 4 5 3 4 3 4 2 2 3 4" };

    int row{4}, col{5}, totalIntensity{3}, matrix[MAXRWOS][MAXCOLS]{}, subMatrix[MAXRWOS][MAXCOLS]{};

    // <--- Needs a prompt.
    //std::cin >> row >> col;
    
    for (int mxRow = 1; mxRow <= row; mxRow++)
    {
        for (int mxCol = 1; mxCol <= col; mxCol++)
        {
            // <--- Needs a prompt.
            //std::cin >> matrix[mxRow][mxCol];

            mxSS >> matrix[mxRow][mxCol];
        }
    }
    
    // <--- Needs a prompt.
    //std::cin >> totalIntensity;
    
    for (int subRow = 0; subRow < row - 1; subRow++)
    {
        for (int subCol = 0; subCol < col - 1; subCol++)
        {
            // <--- Needs a prompt.
            //std::cin >> subMatrix[subRow][subCol];

            subMxSS >> subMatrix[subRow][subCol];
        }
    }
 
    return 0;  // <--- Not required, but makes a good break point.
}

Lines 2, 11 and 12 are just for testing. You do not need them. This just means that you do not have to type something with the "cin" statements every time the program runs leaving more time to focus on the rest of the code.

All of your for loops start at (1). C++ like other languages are (0)zero based, so your arrays will start at (0) not (1). Starting at (1) you are skipping element (0) of the array. Sometimes this can be useful, but most of the time it is not.

Also it is a good policy to initialize your variables when defined.

Notice the changes in the second for loop. This will work better for you, but you still can make better use of the for loop. My idea is to get it working properly with what you are more likely to understand first.

I moved lines 30 and 31 to after the 2nd for loop because it has nothing to do with the 2nd for loop.

This should get you started. after that you need to follow the directions and finish your code.

Andy
Mar 1, 2021 at 2:16pm
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
#include <iostream>
#include <sstream>
#include <valarray>
using namespace std;

istringstream in(
"4 5            \n"
"-1 -1 -4 -4 -1 \n"
"-1 -1 -5 -5 -1 \n"
"-1 -1 -1 -9 -1 \n"
"-1 -6 -7 -8 -1 \n"
"3              \n"
"1 1 4 5        \n"
"3 4 3 4        \n"
"2 2 3 4        \n" );

int main()
{
   int N, M, G;
   int i1, j1, i2, j2;

   in >> N >> M;
   valarray<int> A(N*M);
   for ( int i = 0; i < N * M; i++ ) in >> A[i];
   in >> G;
   for ( int i = 0; i < G; i++ )
   {
      in >> i1 >> j1 >> i2 >> j2;
      cout << valarray<int>( A[gslice( (i1-1)*M+(j1-1), {i2-i1+1,j2-j1+1}, {M,1} )] ).sum() << '\n';
   }
}


-60
-9
-22


Last edited on Mar 1, 2021 at 2:55pm
Mar 2, 2021 at 8:10am
You use files and it doesn't work for me.
Mar 2, 2021 at 8:21am
swingby wrote:
You use files

I'm not using files. You can change "in" to "cin" at all its occurrences in main() if you want to. It's just set up for demonstration purposes with a stringstream so that it can be run in the online compiler without much user effort.


swingby wrote:
it doesn't work for me.

If you have a really pedantic compiler then you can try the following.

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
#include <iostream>
#include <sstream>
#include <valarray>
using namespace std;

istringstream in(
"4 5            \n"
"-1 -1 -4 -4 -1 \n"
"-1 -1 -5 -5 -1 \n"
"-1 -1 -1 -9 -1 \n"
"-1 -6 -7 -8 -1 \n"
"3              \n"
"1 1 4 5        \n"
"3 4 3 4        \n"
"2 2 3 4        \n" );

int main()
{
   unsigned N, M, G;
   unsigned i1, j1, i2, j2;

   in >> N >> M;
   valarray<int> A(N*M);
   for ( size_t i = 0; i < N * M; i++ ) in >> A[i];
   in >> G;
   for ( size_t i = 0; i < G; i++ )
   {
      in >> i1 >> j1 >> i2 >> j2;
      cout << valarray<int>( A[gslice( (i1-1)*M+(j1-1), {i2-i1+1,j2-j1+1}, {M,1ul} )] ).sum() << '\n';
   }
}

Last edited on Mar 2, 2021 at 8:29am
Topic archived. No new replies allowed.