S
S
s2sk2018-03-29 09:03:46
C++ / C#
s2sk, 2018-03-29 09:03:46

Indexes in in turn into array cells?

Hi all.
Something rigidly stupid and I can not implement the following:
I have a cycle and there is a two-dimensional array. From that it is necessary to write down in the first.
Now it looks like this:

for (int i = 0; i < countInd; i++)
  {
    ind[i] = new int[3];

    /* тут код заполнения массива который выше */

    index[i == 0 ? i : i * 3] = ind[i][0];
    index[i == 0 ? i + 1 : i * 4] = ind[i][1];
    index[i == 0 ? i + 2 : i * 5] = ind[i][2];

    char str[256];
    sprintf(str, "array %d %d %d - %d %d %d\r\n", i == 0 ? i : i * 3, i == 0 ? i + 1 : i * 4, i == 0 ? i + 2 : i * 5, ind[i][0], ind[i][1], ind[i][2]);
    Log::Write(str);
  }

The following is in the log:
array 0 1 2 - 0 1 2

array 3 4 5 - 0 2 3

array 6 8 10 - 0 5 4

array 9 12 15 - 0 1 5

array 12 16 20 - 1 5 2

array 15 20 25 - 6 5 2

array 18 24 30 - 6 2 7

array 21 28 35 - 7 2 3

array 24 32 40 - 7 3 0

array 27 36 45 - 7 0 4

array 30 40 50 - 4 5 6

array 33 44 55 - 6 7 4

I'm interested in the numbers before "-". As you can see, at first they go normally "0 1 2 3 4 5 6", and then instead of 7 it already goes 8, and instead of 9 it goes 10, etc. And I need them all to go normally "1 2 3 4 5 6 7 8 9 10 11, etc." I understand why this is happening, but I can not find a normal solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlov, 2018-03-29
@s2sk

You need to replace with , and with . A general example of accessing an array by a single index. In this case, there is no need to check "i" for "0".i * 4i * 3 + 1i * 5i * 3 + 2

int count = 3; // Количество столбцов в двумерном массиве
int row = 2; // Номер строки в двумерном массиве
int col = 2; // Номер столбца в двумерном массиве

index[row * count + col] = 10;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question