Imagine there to be a multidimensional array with 2 columns, each column having exact same element count:
int[,] virtualArray = new int[800,2];
If I were to iterate over such array in a single loop - I would then do something like:
int columnCount = 2;
int eachColumnElementCount = 800;
int totalCombinationCount = Convert.ToInt32(Math.Pow(eachColumnElementCount, columnCount)); // 640_000
for(int i = 0; i < totalCombinationCount ; i++) {
int columnOneIndex= index / totalCombinationCount ;
int columnTwoIndex= index - totalCombinationCount * eachColumnElementCount;
}
The results will be:
0,0 (i = 0)
0,1
..
0,799 (i = 799)
1,0 (i = 800)
1,1
..
1,799
..
799,799 (i = 640_000 - 1)
Now, I would like to extend my current implementation, to iterate in a single-dimensional loop - over a multidimensional array with 3 columns! I.e. the expected result, given, that we have the same 800 element count in each column, shall be following:
int[,] virtualArray = new int[800,3];
int columnCount = 3;
int eachColumnElementCount = 800;
int totalCombinationCount = Convert.ToInt32(Math.Pow(eachColumnElementCount, columnCount)); // 512_000_000
for(int i = 0; i < totalCombinationCount ; i++) {
// int index1 = 0; // ??
// int index2 = 0; // ??
// int index3 = 0; // ??
}
0,0,0 (i = 0)
0,0,1
...
0,0,799
...
10,80,156
...
799,799,799 (i = 512_000_000 - 1)
I can not come up with a formula for the case when there are 3 columns.. Is there a way to loop over such array in a single loop?