I have a general question about searching inside a two-dimensional array. I'm working on a number search program that allows the user to enter a number to search in a grid of numbers. For example:

User wants to search: 234567

In a grid of:
232772725432
734234657337
272425672442
235533655737
252444474436
737533255325
656222376424
573233663633
325534255252
454456544442
237537362337
234562732722

The thing is I want to search backwards and diagonally also. I can easily come up with a sorting algorithm to find backwards and forwards vertically and horizontally but I have no idea how I can search diagonal. Would I use a while loop instead of a for loop so that I can increment each index of the two-dimensional array? That is what I have so far and it works sometimes but not all the time. Any suggestions would help. Thanks

Diagonal search (left -> right, top -> bottom):

//Consider:
searchStr; // the string (array) we need to search for
2dArray; // the array in which we are searching
// start search
for (row = 0; row < (2dArray.length - searchStr.length); row++){
   for (col = 0; col < (2dArray.length - searchStr.length); col ++){
      strFound = true; // consider the string found
      for (k = 0; k < searchStr.length;  k++){
         if (searchStr[k] != 2dArray[row+k][col+k]){
            strFound = false; break;
         }
      }
      if (strFound){
         // string was found. Do something
      }
   }
}

GL

Diagonal search (left -> right, top -> bottom):

//Consider:
searchStr; // the string (array) we need to search for
2dArray; // the array in which we are searching
// start search
for (row = 0; row < (2dArray.length - searchStr.length); row++){
   for (col = 0; col < (2dArray.length - searchStr.length); col ++){
      strFound = true; // consider the string found
      for (k = 0; k < searchStr.length;  k++){
         if (searchStr[k] != 2dArray[row+k][col+k]){
            strFound = false; break;
         }
      }
      if (strFound){
         // string was found. Do something
      }
   }
}

GL

Thanks for the help but I noticed an awkward error in your code. I tested out different search strings and found a weird pattern in errors. For example when I search the string 232543365432, a 12 digit number found diagonally from the top left corner to the bottom right corner returns false. But when I search 23254336543, a 11 digit number one less than the full 12 digit, it returns true, which is supposed. Do you have any answer why it's returning false for that 12 digit number when it does exist in the grid?

try this

for (row = 0; row <= (2dArray.length - searchStr.length); row++){
   for (col = 0; col <= (2dArray.length - searchStr.length); col ++){
// row and col <= not <

if it's not working do a system.output to see what happens at each step

try this

for (row = 0; row <= (2dArray.length - searchStr.length); row++){
   for (col = 0; col <= (2dArray.length - searchStr.length); col ++){
// row and col <= not <

if it's not working do a system.output to see what happens at each step

Thanks for the help. After spending hours going through step by step I noticed the < has to be <=. Thanks for the help though, much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.