Methods II
[Framework practical 4 of 7]


Ok, so the answer is that in the 1D array you need to count across the number of values associated with the rows in the 2D array you've already completed, and add on the number of values you've processed in the current row.

In the algorithm:

// Make a double tempArray [reranged.length * reranged[0].length]

// Open loop with index i down reranged rows
   // Open loop with index j across a row
      // tempArray[????] = reranged[i][j]
   // End loop across row
// End loop across
// return tempArray

the width of one row is reranged[0].length, and the number of rows you've completely processed is i. Remember, you don't want to add on the width of the current row as you're only part-way across it. The cells processed in previous rows is thus: reranged[0].length * i.

If you're not convinced (perhaps because you think i is the current row count, rather than the rows that have been processed), consider the situation where i == 1. When this is the case, reranged[0].length * i represents the values in the first "zero" row, which is the row that has been completed, and none of the current row. This is one of those nice examples that works because array indices start at zero, not one.

The distance across the current row is j. Again, because indices start at zero, although j is counting the values looped through, it also gives the correct position for the next value.

So, the full answer is:

tempArray[(reranged[0].length * i) + j] = reranged[i][j]

If you're still unsure, work through the algorithm by hand using a 2x3 array on paper, writing down the i, j, and (reranged[0].length * i) + j for each outer and inner loop run.