Hello, I have a 2D array of 10x10 and I want to search the surrounding cells of only a 3x3 portion of it at a time. How can I go about totaling up the values of the following nested loop? There should be a 1 or a 0 and if it's a 1 that is found, I want to keep track of it and total them up. Then from there I will apply some more logic depending on the total found. This is just a snippet of the problem area and everything else seems fine, there are no errors in the rest of the code. Thank you!

``````int row = 0;
int col = 0;

for(int x = row - 1; x < row + 1; x++)
{

for(int y = col - 1; y < col + 1; y++)
{

if(x <= 1)
{
x = 0;

}

if(y <= 1)
{
y = 0;

}

}
}``````

Hello, I have a 2D array of 10x10 and I want to search the surrounding cells of only a 3x3 portion of it at a time. How can I go about totaling up the values of the following nested loop? There should be a 1 or a 0 and if it's a 1 that is found, I want to keep track of it and total them up. Then from there I will apply some more logic depending on the total found. This is just a snippet of the problem area and everything else seems fine, there are no errors in the rest of the code. Thank you!

``````int row = 0;
int col = 0;

for(int x = row - 1; x < row + 1; x++)
{

for(int y = col - 1; y < col + 1; y++)
{

if(x <= 1)
{
x = 0;

}

if(y <= 1)
{
y = 0;

}

}
}``````

Here is what I've come up with but it's not correct yet. I'm not sure if it's my logic or the way I'm passing it. Still I have no errors and when I run it I only get zeros.

``````int countArray(int offScreenArray[10][10])
{
int row = 0;
int col = 0;
int sum = 0;

for(int x = row - 1; x < row + 1; x++)

for(int y = col - 1; y < col + 1; y++)
{
sum = offScreenArray[x][y];
sum++;

if(sum <= 1)
{
sum = 0;

}

if(sum > 1)
{
sum = 1;

}

}

return sum;
}``````

This is my call-

``````for(int row = 0; row < 10; row++)
{

countArray(offScreenArray);
cout << sum << "\t";

for (int col = 0; col < 10; col++)
{

countArray(offScreenArray);
cout << sum << "\t";

}
cout << endl;

}``````

As you can see from my call, I'm trying to iterate through each row and column and print them out. But my guess is that I'm doing this wrong and I most likely should incorperate these 2 calls somehow. Again, I'm not receiving any compiling errors and if it seems like something is missing such as a declaration, I probably have it but didn't show it here. Any help or clues will be greatly appreciated. Thank you!

In the countArray function you initialize row and col to 0 then u minus 1 from them,that gives you -1, I think you are going out of bounds.

In the countArray function you initialize row and col to 0 then u minus 1 from them,that gives you -1, I think you are going out of bounds.

I see what you mean. How do I get around this? I'm trying to pass my array to this function and without initiating the row and col I get errors. Do I just name them something else and when I pass to the function it will just use the value? I get confused and do not totally understand functions just yet. Thank you!

>>I have a 2D array of 10x10 and I want to search the surrounding cells of only a 3x3 portion of it at a time

I'd use the corners of the 3x3 section and then 4 loops to get the sum of cells surrounding the 3x3 section. Since this doesn't look like a member methoh you can get by passing the larger 10x10 grid and enough information to determine the 3x3 section you want to evaluate. In order to get the sum you are after you want to evaluate the row above and below as well as the row immediately to the left and right. In order to determine that information you will need to know all 4 corners of the 3x3 grid or 2 diagonally opposed corners or any given corner, as long as you know which one it is. Then you have to decide if you want to include the 4 cells diagonal to the corners to the 3x3 grid or just those cells that are orthagonal to the 3x3 grid. Once you know the desired rows and columns you can use a running total passed a loop to count all 12 (or 16 as the case may be) cells surrounding the 3x3 section.

``````int sumSurround(int** t, int r, int c)
//t = 10x10 table, r and c specifiy upper left cell of 3x3 section to evaluate assuming cell(0, 0) is upper left cell of t.

//count all 16 cells surrounding the 3x3 section using the following four loops storing the value in sum.
sum = 0;

top row to count = r - 1 from column c - 1 to c + 4
sum += current cell value

bottom row to count = r + 3 from column c - 1 to c + 4
sum += current cell value

left column to count = c - 1 from r to r + 2
sum += current cell value

right column to count = c + 1 from r to r + 2
sum += current cell value

return sum``````

>>I have a 2D array of 10x10 and I want to search the surrounding cells of only a 3x3 portion of it at a time

I'd use the corners of the 3x3 section and then 4 loops to get the sum of cells surrounding the 3x3 section. Since this doesn't look like a member methoh you can get by passing the larger 10x10 grid and enough information to determine the 3x3 section you want to evaluate. In order to get the sum you are after you want to evaluate the row above and below as well as the row immediately to the left and right. In order to determine that information you will need to know all 4 corners of the 3x3 grid or 2 diagonally opposed corners or any given corner, as long as you know which one it is. Then you have to decide if you want to include the 4 cells diagonal to the corners to the 3x3 grid or just those cells that are orthagonal to the 3x3 grid. Once you know the desired rows and columns you can use a running total passed a loop to count all 12 (or 16 as the case may be) cells surrounding the 3x3 section.

``````int sumSurround(int** t, int r, int c)
//t = 10x10 table, r and c specifiy upper left cell of 3x3 section to evaluate assuming cell(0, 0) is upper left cell of t.

//count all 16 cells surrounding the 3x3 section using the following four loops storing the value in sum.
sum = 0;

top row to count = r - 1 from column c - 1 to c + 4
sum += current cell value

bottom row to count = r + 3 from column c - 1 to c + 4
sum += current cell value

left column to count = c - 1 from r to r + 2
sum += current cell value

right column to count = c + 1 from r to r + 2
sum += current cell value

return sum``````

Hello, I'm sorry but I think I misled you. What I want to do is count the total of only a 3x3 grid at a time that is inside of the 10x10 grid, Please forgive me I really appreciate your helping me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.