Wallace Mogerty Wallace Mogerty - 2 months ago 7
C++ Question

Get Index Given Row and Column

Is there a formula that can be used to get the index given the current column and row? The following is what I have so far. The problem is, however, that the code only works for the first row...

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
index = i * (columns - 1) + j;
}
}

Answer

This should work:

        index = i * columns + j;