I’m wondering what int id = 5%2; does exactly and also look for similar things.
Reason:
I want to calculate by a number on which row / column the item should be standing.
[Example]
I have a grid which is 5×5.
If id = 05, it should be on the 1st row and the 5th column
If id = 10, it should be on the 2nd row and the 5th column
If id = 12, it should be on the 3rd row and the 2nd column
How you catch my drift!
(ps: feel free to edit my tags. Not sure what to put on this question)
The modulus (
%in some C-derivative languages) is the remainder left over when one number is divided by another. So38 % 6is2(38 / 6is6with a remainder of2).It’s typically used for exactly the sort of thing you’re asking about. If your 5×5 grid is:
then the row can be calculated as
(x-1)/5+1(that’s integer division rather than floating point) and the column as(x-1)%5+1:The reason you initially subtract the one and then add it on is because modulus works best on zero-based numbers while yours are one-based. The subtract/add is to turn your scheme into zero-based before performing the modulus, then turning it back into one-based afterwards.