Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 560009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:17:52+00:00 2026-05-13T12:17:52+00:00

I have an array (of 9 elements, say) which I must treat as a

  • 0

I have an array (of 9 elements, say) which I must treat as a (3 by 3) square.
For the sake of simplifying the question, this is a one-based array (ie, indexing starts at 1 instead of 0).

My goal is to determine valid adjacent squares relative to a starting point.

In other words, how it’s stored in memory: 1 2 3 4 5 6 7 8 9

How I’m treating it:
7 8 9
4 5 6
1 2 3

I already know how to move up and down and test for going out of bounds (1 >= current_index <= 9)

edit: I know the above test is overly general but it’s simple and works.

//row_size = 3, row_step is -1, 0 or 1 depending on if we're going left,
//staying put or going right respectively.
current_index += (row_size * row_step); 

How do I test for an out of bounds condition when going left or right? Conceptually I know it involves determining if 3 (for example) is on the same row as 4 (or if 10 is even within the same square as 9, as an alternate example, given that multiple squares are in the same array back to back), but I can’t figure out how to determine that. I imagine there’s a modulo in there somewhere, but where?

Thanks very much,
Geoff

Addendum:
Here’s the resulting code, altered for use with a zero-based array (I cleaned up the offset code present in the project) which walks adjacent squares.

bool IsSameSquare(int index0, int index1, int square_size) {
  //Assert for square_size != 0 here
  return (!((index0 < 0) || (index1 < 0))
      && ((index0 < square_size) && (index1 < square_size)))
      && (index0 / square_size == index1 / square_size);
}
bool IsSameRow(int index0, int index1, int row_size) {
  //Assert for row_size != 0 here
  return IsSameSquare(index0, index1, row_size * row_size)
  && (index0 / row_size == index1 / row_size);
}
bool IsSameColumn(int index0, int index1, int row_size) {
  //Assert for row_size != 0 here
  return IsSameSquare(index0, index1, row_size * row_size)
      && (index0 % row_size == index1 % row_size);
}

//for all possible adjacent positions
for (int row_step = -1; row_step < 2; ++row_step) {
  //move up, down or stay put.
  int row_adjusted_position = original_position + (row_size * row_step);
  if (!IsSameSquare(original_position, row_adjusted_position, square_size)) {
    continue;
  }
  for (int column_step = -1; column_step < 2; ++column_step) {
    if ((row_step == 0) & (column_step == 0)) { continue; }
    //hold on to the position that has had its' row position adjusted.
    int new_position = row_adjusted_position;

    if (column_step != 0) {
      //move left or right
      int column_adjusted_position = new_position + column_step;
      //if we've gone out of bounds again for the column.
      if (IsSameRow(column_adjusted_position, new_position, row_size)) {
        new_position = column_adjusted_position;
      } else {
        continue;                          
      }
    } //if (column_step != 0)
    //if we get here we know it's safe, do something with new_position
    //...
  } //for each column_step
} //for each row_step
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:17:53+00:00Added an answer on May 13, 2026 at 12:17 pm

    This is easier if you used 0-based indexing. These rules work if you subtract 1 from all your indexes:

    • Two indexes are in the same square if (a/9) == (b/9) and a >= 0 and b >= 0.
    • Two indexes are in the same row if they are in the same square and (a/3) == (b/3).
    • Two indexes are in the same column if they are in the same square and (a%3) == (b%3).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an array, @theArr, which holds 1,000 or so elements such
Let's say I have an array of elements for which a total ordering exists.
I have an array, whose size is 256. This array has elements which are
say, i have a string array of 50000 elements. Searching the array using For
Consider I have an array of elements out of which I want to create
VISUAL C++ Question Hi, I have array of 3 elements and I want to
I have a large array (say 512K elements), GPU resident, where only a small
I have an object array, I know that the elements are type String, say
Say I have an array with a couple hundred elements. I need to iterate
So, say I have an array that looks like this: t = [ [

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.