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

  • SEARCH
  • Home
  • 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 746295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:04:28+00:00 2026-05-14T14:04:28+00:00

If I have a 4×4 gameboard which I’m representing in my program as a

  • 0

If I have a 4×4 gameboard which I’m representing in my program as a 1d integer array of size 16.

How can I get the indexs of the squares above, below, to the left and to the right any given index?

So, for example:

A = { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 }

Which represents this board

 0   1   2   3
 4   5   6   7
 8   9  10  11
12  13  14  15  

Lets say I am currently on index #8 in the board (value = 7). How can I get the index for 4 (value = 3) , 5 (value = 6), 10 (value = 11) and realize that there is no right square because it is on the right hand edge of the board.

I know I need to use some modulus math but I’m failing to come up with the right way to get the indexes for adjacent squares.

I’m thinking something like…

if ((i % 4) + 1 < 3) right = i + 1;
if ((i % 4) - 1 > 0) left = i - 1;
if ((i % 4) + 4 < 15) bottom = i + 4;
if ((i % 4) - 4 > 0 ) top = i - 4;

Does this seem like it is the right approach?

  • 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-14T14:04:29+00:00Added an answer on May 14, 2026 at 2:04 pm

    To get row,column from your index, use the following:

    row = index/num_columns;
    column = index % num_columns;
    

    to get back to the index, use

    index = row * num_columns + column;
    

    One you’re in rows and columns, it’s easy to get the surrounding positions.

    above = (row-1, column)
    left = (row, column-1)
    etc...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a header file in which there is a large struct. I need
I have the following numpy array: # A B C Y my_arr = np.array([
I have another one problem with pop3. Here is connection to pop3-server: $pop3Server =
I have a small (500kb) swing applet that displays very simple/limited set of small
I have inherited some code and am getting an error when I try to
I have successful executed following commands: tar xzvf fcgi-2.4.0.tar.gz cd fcgi-2.4.0 ./configure --prefix=/usr/local/fcgi make
in C#,I have a double variable price with value 10215.24. I want to show
I have to put my database's data in this format (values will differ, obviously),
I have an XML file with a lot of nodes similar to the following
I have some data that needs to be validated, and while I have found

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.