# Thread: Identifying 3x3 blocks (2d-array)

1. ## Identifying 3x3 blocks (2d-array)

hey guys,
i'm working on a program right now that reads 2 types of data in from a file and stores it into two different 2-d arrays. after the read in, i tried to group the arrays into 3x3 blocks. so there are 210 initial readings with 70 3x3 blocks. after doing this i need to examine each block to see if they are within 1 (reading from file) of each other. I am having trouble with the examining part. i posted my code thus far but am now stuck on how to examine the blocks surrounding the middle one. if anyone has any suggestions please let me know. thanks in advance.

Code:
```int main()
{
double frequency[209][209];
double intensity[209][209];
int count;
int col;
int midA;
int midB;

ifstream inFile;
inFile.open("C:\\project6\\sensory-image.txt");

for(count=0;count<210;count++)
{
for(col=0;col<210;col++)
{
inFile >> frequency[count][col] >> intensity[count][col];
}
}
inFile.close();

for(midA=1;midA<71;midA++)
{
frequency[midA][midD] = frequency[count+1][col+1];
intensity[midA][midD] = frequency[count+1][col+1];
count = count+3;
for(midB=1;midB<71;midB++)
{
frequency[midA][midD] = frequency[count+1][col+1];
intensity[midA][midD] = frequency[count+1][col+1];
col = col+3;
}
}```

2. > for(count=0;count<210;count++)
This runs off the end of the array. Use < N, where N is the dimension of the array you're subscripting.
Or if there really are 70*3 elements, increase the size of your arrays to 210.

Code:
```a b c  --> 69 more this way
d e f
g h i
|
|
v
69 more this way```
Can you describe what you're trying to do with values 'a' to 'i' in this diagram.

Code:
```for ( r = 0 ; r < 210 ; r += 3 ) {
for ( c = 0 ; c < 210 ; c += 3 ) {
// r and c index the 'a' element of each 3x3 cell
}
}```

3. i am trying to take e, and compare it to everything around it, ie a b c d f g h i to see if e is within one of everything around it. if it is within one then i want to display that block number, so (1,1) or wherever it may come up. and then look at the next block of 3x3 and so on and so forth.i will change the array size thanks

would i have to make a nested loops to examine each letter of ur diagram and include an if statement for the comparisons?

4. Something like
Code:
```for ( r = 0 ; r < 210 ; r += 3 ) {
for ( c = 0 ; c < 210 ; c += 3 ) {
if ( isWithin( frequency, r, c ) && isWithin( intensity, r, c ) ) {
printf( "Found\n" );
}
}
}```
Have a go at writing the isWithin() function.

5. You are supposed to define it yourself.