Thread: Searching through a grid of numbers

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Question Searching through a grid of numbers

    Hello:
    Right now say; I have a 12x6 grid full of numbers from 1-999.
    If I want to find a sequence of numbers in this grid that exists in a straight line in the following 8 directions: North, North East, East, South East, South, South West, West, and North West... how would I do this?
    Whats the best way to loop through the inputted sequence and search for it in the grid?
    I am completely lost here so any help would be greatly appreciated!

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    I would have a one-dimensional array to hold your sequence.

    int sequence[SEQ_SIZE];

    I would have a two-dimensional array to hold your grid of numbers.

    int grid[12][6];

    Work through the two-dimensional array from left to right and top to bottom. Check each number in the grid to see if it matches the first number in the sequence. If it does, search around that grid location to see if you can find the second number in the sequence. If you find the second number, search around that grid location to find the third number. If you don't find the next number in the sequence, keep moving down the grid until you come to the next instance of the first number of the sequence you're searching for. Rinse and repeat until all numbers in the sequence have been found.

    Since you know the current location you're searching, simple addition or subtraction to the row and col numbers (i.e. grid[row-1][col+1] to search north east) will get you the other locations you need to search. I would be sure to check to make sure that the row and col numbers are in bounds even after you add or subtract from them, however.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Did you check your PM's? I sent you a PM about this yesterday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  5. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM