7 Replies - 1983 Views - Last Post: 24 September 2010 - 05:43 AM Rate Topic: -----

#1 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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?
Posted Image


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  Icon User is offline

  • code > sword
  • member icon

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

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

Posted 22 September 2010 - 11:49 PM

View Postgretty, 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?
Was This Post Helpful? 0
  • +
  • -

#3 gretty  Icon User is offline

  • D.I.C Head

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

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

Posted 22 September 2010 - 11:59 PM

View Postjanotte, on 22 September 2010 - 10:49 PM, said:

View Postgretty, 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 :)
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • 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";
)
Was This Post Helpful? -3
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • 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.

:code:

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

Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#8 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • 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;


:chris:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1