Thread: Finding A Smaller Array Within A Bigger Array (2D)

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

    Finding A Smaller Array Within A Bigger Array (2D)

    I've got two 2D arrays declared and I want to write some code to find the smaller one within the bigger one. Any tips on how this could work? I know it involves something like looping the values of the bigger array, then the smaller one. Thanks for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,079
    You start by drawing the problem out on paper, then figuring out what you might need.
    Code:
    int large[4][4] = {
        {  1,  2,  3,  4 },
        {  5,  6,  7,  8 },
        {  9, 10, 11, 12 },
        { 13, 14, 15, 16 }
    };
    int small[2][2] = {
        {  7,  8 },
        { 11, 12 },
    };
    Your first exercise is write code that finds small[0][0] somewhere in large.
    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
    Dec 2021
    Posts
    34
    I've tried this based on another post I've found on this subject. Where head[1].height/width is for the big array and head[2].height/width is for the smaller (query) array. i and j are width and height for the big array and qi and qj are width and height for small array...
    Code:
    for (j=0;j<head[1].height-head[2].height;j++)
        {
            for (i=0;i<head[1].width-head[2].width;i++)
            {
                QueryArray[j][i] != NULL;
    
    
                for (qi=0;qi<head[1].width;qi++)
                {
                    for (qj=0;qj<head[1].height;qj++)
                    {
                        if ((head[1])[j+qj][i+qi] != QueryArray[j][i])
                        {
                            QueryArray == NULL;
                            break;
                        }
                    }
    
    
                    if(!QueryArray) break;
                }
                if(QueryArray) return;
            }
            return;
        }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,079
    At some point you need to be able to write your own code from first principles.

    Not just repost "found" code.

    Especially when said "found" code is full of nonsense.
    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
    Registered User
    Join Date
    Dec 2021
    Posts
    34
    Well if I'm stuck I generally look it up and repurpose found code to better understand it so I can do it myself.

  6. #6
    Registered User
    Join Date
    Dec 2021
    Posts
    34
    Though I believe the for loops in the code are at least correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-01-2017, 12:42 PM
  2. why does my array get bigger?
    By slenkar in forum C Programming
    Replies: 6
    Last Post: 11-08-2010, 03:14 PM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. finding the least bigger number from the tree
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 03:34 PM
  5. make array smaller...bad data i think
    By t014y in forum C Programming
    Replies: 2
    Last Post: 01-27-2009, 02:32 PM

Tags for this Thread