tags:

views:

37

answers:

2

How do you get row back out of n?

local n = row * cols + col
local c = n % cols
local r = ?
+4  A: 

Using some simple arithmetic...

You have

n = rows * cols + col

subtract col from both sides

n - col = rows * cols

divide by cols on both sides

(n - col) / cols = rows

Assuming col < cols holds, you could do it with integer division as rows = n / cols.

aioobe
Algebra betrays me ;) I've got dyscalculia. Yet I'm a programmer. Go figure. I just like making computers do things. Some interesting stuff is out of my reach, but I'm happy with what I do. Besides that's what Stackoverflow is all about right? :) I admit I ask more questions than I answer.
Scott
A: 

local r = int( n / cols )

shibblez
there's always a dilemma in cases like this. Downvote someone with 1 rep? It just doesn't make sense. SO should allow negative rep.
aaronasterling
oh well. I'll do it anyway. -1 You need to work on algebra.
aaronasterling
I'm upvoting. @aaronasterling, You should be *very* careful when down-voting. There's nothing wrong with this answer. It's perhaps even preferable over mine. Since the current column (`col`) will always be less than the number of columns (`cols`), taking the integer part of `n / cols` is perfectly valid. You can't take back your down-vote unless he edits the answer, so let this situation be a lesson for down-voting.
aioobe