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

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    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?
    Last edited by Shapper; 04-29-2007 at 12:35 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are supposed to define it yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM