What I am trying to do is calculate the row and column of which an id is in. For example I have the grid

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

As you can see this is a 4x4 grid. I work out the row number by getting the id then dividing by the number of columns, and rounding this number down. For example rownumber = 5(id value)/4(number of columns) = 1.25 then round down = 1. But what I can't figure out is how to get the column number of say the number 6.

Any help?

  • 1
    $\begingroup$ The operation you want is "mod", or possibly "rem" for "remainder", depending on the specific program you're using. If you want the column number of 5, you evaluate 5 mod 4 = 1 (sometimes written as mod(5,4)). If you don't have access to "mod" or "rem", you can do it by getting the row number, multiplying by the number of columns, and subtracting it from the id. So 5 - 4*1 = 1. $\endgroup$ – Glen O Apr 28 '15 at 13:33

If $n$ is the number in the table

the row is so calculated:

$n/4$ rounded to the lower integer if decimal.

while the column is:

$n \bmod(4)$ where $\bmod(4)$ means the remainder of the division by 4.

| improve this answer | |
  • $\begingroup$ Rounding is often not an inbuilt function, so $n-n \bmod(4)$ will directly give the row number. $\endgroup$ – ghosts_in_the_code Apr 28 '15 at 15:05
  • $\begingroup$ @ghosts_in_the_code Good point. If this has to be implemented, he could simply do (int)(n/4) in C++ or anything equivalent in other programming languages. Btw, this is a bit off-topic, as the question is... $\endgroup$ – leoll2 Apr 28 '15 at 15:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.