Home All Groups Group Topic Archive Search About
Author
16 Nov 2007 1:05 PM
John Stulle
Hello,

I have a grid like data array which starts at Index 0 and ends at end 14.

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

Can I ask how you can savely determine the row and column for a given
index??
For example I would like to know which row and column index 7 would be.
My brain can "see" that in my grid index 7 would be row 2 and column 3,
but I just can't find the math formula behind that.
Can anybody help please?

Thank you very much.
John

Author
16 Nov 2007 1:33 PM
David Kerber
In article <eeA0yFFKIHA.3***@TK2MSFTNGP02.phx.gbl>,
j.stulleNOSPAM@googlemail.com says...
Show quote
> Hello,
>
> I have a grid like data array which starts at Index 0 and ends at end 14.
>
> 0,1,2,3,4
> 5,6,7,8,9
> 10,11,12,13,14
>
> Can I ask how you can savely determine the row and column for a given
> index??
> For example I would like to know which row and column index 7 would be.
> My brain can "see" that in my grid index 7 would be row 2 and column 3,
> but I just can't find the math formula behind that.
> Can anybody help please?
>
> Thank you very much.
> John

Does your grid always have 5 columns?  Are the numbers always sequential
starting at 0?  Is your grid 0-based or 1-based?

If both of these are true, and it's a 1-based grid index, then divide
the number by 5, take the integer part of that, and add 1; that gives
you the row.  Then take the remainder of the original division, and that
gives you the column.

If the grid is 0-based, then subtract 1 from the result of each of the
above calculations.

I'll leave the actual code writing for the above algorithm as an
exercise for the student ;-)

--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).
Author
16 Nov 2007 2:33 PM
John Stulle
Thanks!
Author
16 Nov 2007 5:10 PM
Pop`
David Kerber wrote:
Show quote
> In article <eeA0yFFKIHA.3***@TK2MSFTNGP02.phx.gbl>,
> j.stulleNOSPAM@googlemail.com says...
>> Hello,
>>
>> I have a grid like data array which starts at Index 0 and ends at
>> end 14.
>>
>> 0,1,2,3,4
>> 5,6,7,8,9
>> 10,11,12,13,14
>>
>> Can I ask how you can savely determine the row and column for a given
>> index??
>> For example I would like to know which row and column index 7 would
>> be. My brain can "see" that in my grid index 7 would be row 2 and
>> column 3, but I just can't find the math formula behind that.
>> Can anybody help please?
>>
>> Thank you very much.
>> John
>
> Does your grid always have 5 columns?  Are the numbers always
> sequential starting at 0?  Is your grid 0-based or 1-based?
>
> If both of these are true, and it's a 1-based grid index, then divide
> the number by 5, take the integer part of that, and add 1; that gives
> you the row.  Then take the remainder of the original division, and
> that gives you the column.
>
> If the grid is 0-based, then subtract 1 from the result of each of the
> above calculations.
>
> I'll leave the actual code writing for the above algorithm as an
> exercise for the student ;-)

Thanks from here, too; I can use that.

Pop`

AddThis Social Bookmark Button