# Find a Cell's Column & Row from its index

Page 1 of 1

## 7 Replies - 2351 Views - Last Post: 24 September 2010 - 05:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=191611&amp;s=2b529d77992020da6ecb9a21de2aafc4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gretty

Reputation: 3
• Posts: 123
• Joined: 25-May 09

# Find a Cell's Column & Row from its index

Posted 22 September 2010 - 11:26 PM

Hello

I have a 1d array of ints ( int boardCells[49]; ) which models cells on a game board(see below picture).

My Problem:
Given the index of an element in boardCells, I need to find out what column that cell is in & what row that cell is in. I know that I could just use a 2d array & it would be alot easier but I am rewritting a large program & I will have to go through everything to if I change from a 1d to 2d array.

So I am trying to use some maths(not my strong point) to find the column & row associated with a cell index. In my picture below the cell boardCells[ 10 ] would be selected, but what formula can I use to find out what column & row boardCells[ 10 ] is in?

Also my picture is not that correct because in the program the number of columns always equals the number of rows(whereas in the picture there are 7 columns & 4 rows) but the picture is just an example.

Would maybe some formula like this work, (again math is not my strong point):

int column = selectedCellIndex % totalNumOfCells;
int row = selectedCellIndex % totalNumOfCells;

This post has been edited by gretty: 22 September 2010 - 11:57 PM

Is This A Good Question/Topic? 0

## Replies To: Find a Cell's Column & Row from its index

### #2 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Find a Cell's Column & Row from its index

Posted 22 September 2010 - 11:49 PM

gretty, on 23 September 2010 - 03:26 PM, said:

I have a 1d array of ints ( int boardCells[28]; )

in the program the number of columns always equals the number of rows

Okay so that's difficult.
What is the square root of 28?

### #3 gretty

Reputation: 3
• Posts: 123
• Joined: 25-May 09

## Re: Find a Cell's Column & Row from its index

Posted 22 September 2010 - 11:59 PM

janotte, on 22 September 2010 - 10:49 PM, said:

gretty, on 23 September 2010 - 03:26 PM, said:

I have a 1d array of ints ( int boardCells[28]; )

in the program the number of columns always equals the number of rows

Okay so that's difficult.
What is the square root of 28?

The int boardCells[28]; was just an example but I have changed it 49. In my actual program the number of rows & number of columns will ALWAYS be the same.

Any math formulas suggestions would be really helpful, & appereciated

### #4 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Find a Cell's Column & Row from its index

Posted 23 September 2010 - 12:11 AM

So perhaps a little less time time drawing picture and a little more time writing a meaningful posting, perhaps?

Did the hint of "what is the square root" pass you by completely?
If the rows and cols and are always the same number then the number of rows is the <???> of the array length?
Hint, hint.

EDIT - Fix typo and add some more to make things clearer.

Once you have worked out the number of rows you also know the number of cols and therefore the number of elements in each row.

You friend modulus (%) will then be helpful.

Remember that you will have to account for the fact that array indexes start at 0 not 1 so you may have a bit of manipulation to do with the raw output of (or input to) your maths otherwise you will have a "off by one error" (so common it has it's own acronym OBOE or the alternative OBOB for "off by one bug").

This post has been edited by janotte: 24 September 2010 - 06:23 AM

### #5 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: Find a Cell's Column & Row from its index

Posted 23 September 2010 - 09:30 PM

Forget about 1,2,...D arrays because all arrays are really 1D
Use two for loops one to address rows and the second to address columns and if necessary write to a column square.
So: int Rows=7,Cols=7
for(int i=0;i<Rows+1;i++)
for(int j=0;j<Cols+1;j++)
{
if(i==1 && j==2)
cout<<"You are in 2nd row and 3rd col";
)

### #6 eker676

• Software Engineer

Reputation: 378
• Posts: 1,833
• Joined: 18-April 09

## Re: Find a Cell's Column & Row from its index

Posted 23 September 2010 - 09:39 PM

buffalo: No one can read that. Find the Enter key and use code tags.

A wall of text isn't helpful.

Your last 5 or so posts have been nothing but gibberish.
<Edit> I stand corrected, every post is like that.

Don't take it personally, just format your responses so everyone can read them with ease.

This post has been edited by eker676: 23 September 2010 - 09:44 PM

### #7 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Find a Cell's Column & Row from its index

Posted 23 September 2010 - 11:45 PM

@buffalobill
Everything eker676 said and in addition your post showed you have failed to understand the initial problem posed by the OP.

I think the OP has departed now but if yours was the first post a noob got to the original question that off-topic twaddle you posted could confuse them to the point they waste days of their time and/or get them a fail on an assignment.

A few days back I went through some of your posts and gave you enough positive votes to get you back to zero reputation so you could start fresh and do better than you have been. Take that chance, don't slide back into a negative rep when you are clearly capable of doing better than that if you take a bit more time and apply just a little more thought to your postings.

Live long and prosper!

### #8 Seta00

Reputation: 14
• Posts: 84
• Joined: 22-September 10

## Re: Find a Cell's Column & Row from its index

Posted 24 September 2010 - 05:43 AM

```int column = cell % numberOfColumns;
int row = cell / numberOfColumns;

```

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}