Thread: Finding a smaller array within a larger 2D array

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    34

    Finding a smaller array within a larger 2D array

    I have this code to try and locate a small array within a big array but it seems to crash on the first if statement. Both arrays are dynamically allocated from files and point to a strcuture.
    Code:
    for (i=0;i<head[1].width-head[2].width;i++) //Looks for possible start postions
        {
            int wally = 0;
            //printf("%d ", i);
            for(j=0;j<head[1].height-head[2].height;j++)
            {
                wally = 1; //Tests if query array is found in main input array
                for(qi=0; qi<head[2].height; qi++) //Looks through smaller array for match
                {
                    for(qj=0;qj<head[2].width;qj++)
                    {
                        if ((RGBArray[i+qi][j+qj].red != QueryArray[qi][qj].red) || (RGBArray[i+qi][j+qj].green != QueryArray[qi][qj].green)
                        || (RGBArray[i+qi][j+qj].blue != QueryArray[qi][qj].blue))
                        {
                            wally = 0;
                            break;
                        }
                    }
    
    
                    if(!wally)
                        break;
    
    
                }
                if(wally)
                {
                    identify_y = j; //Stores j values that match in variable
                    identify_x = i; //Stores i values that match in variable
                    printf("%d ", i);
                    printf("%d ", j);
                    break;
                }
            }
        }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    134
    Maybe you should set a breakpoint on line 12 and inspect your arrays.
    Maybe one of your pointers or both are invalid or you access them out of range.

  3. #3
    Registered User
    Join Date
    Dec 2021
    Posts
    34
    Well it appears to run fine when I break before the first if statement but for ((RGBArray[i+qi][j+qj].red != QueryArray[qi][qj].red) || (RGBArray[i+qi][j+qj].green != QueryArray[qi][qj].green || (RGBArray[i+qi][j+qj].blue != QueryArray[qi][qj].blue)) i+qi is the widths of the arrays added together for example but I'm not accessing that really just checking it? And I'm not sure of another way to check if the array isn't in the big one.

  4. #4
    Registered User
    Join Date
    Dec 2021
    Posts
    34
    Though it still crashes even if I change it to i-qi

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,119
    Code:
    for(qj=0;qj<head[2].width;qj++)
    Code:
    for(j=0;j<head[1].height-head[2].height;j++)
    In one place, j seems to be related to width in the other height!

    Which is it?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding A Smaller Array Within A Bigger Array (2D)
    By DGB_684 in forum C Programming
    Replies: 5
    Last Post: 01-04-2022, 11:24 AM
  2. typecast array of bytes to larger data types
    By codegrush in forum C Programming
    Replies: 4
    Last Post: 03-20-2012, 11:39 PM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. make array smaller...bad data i think
    By t014y in forum C Programming
    Replies: 2
    Last Post: 01-27-2009, 02:32 PM
  5. Possible to pack smaller data types into larger ones?
    By Heraclitus in forum C Programming
    Replies: 3
    Last Post: 02-09-2003, 03:22 PM

Tags for this Thread