What is the simpliest way to split 2D array - for example - Split 9x9 2D array to - 3x3 2D arrays (squares).
Thank you!
What is the simpliest way to split 2D array - for example - Split 9x9 2D array to - 3x3 2D arrays (squares).
Thank you!
Another one attempting to write a sudoku program, eh?
A loop or two that copies or moves data from the 9x9 array into the required 3x3 arrays. It also happens to be the most general approach.
Depending on how the 3x3 "squares" are laid out in the 9x9 arrays (e.g. do they overlap? if they do overlap, is it in certain manner?), it is also possible, with a few pointer techniques, to treat a 9x9 array as if it is an array of 3x3 arrays. That is not exactly the simplest of approaches though.
It depends on exactly how you want to split it. For example, if your array is
Then, do you want it split up into left, middle and right, top middle and bottom (like a sudoku)? or something like row and/or column numbers, modulo 3?Code:00 01 02 03 04 05 06 07 08 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 30 31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 60 61 62 63 64 65 66 67 68 70 71 72 73 74 75 76 77 78 80 81 82 83 84 85 86 87 88
Code:// "sudoku style" left, middle and right, top, middle and bottom // top-left 00 01 02 10 11 12 20 21 22 // top-middle 03 04 05 13 14 15 23 24 25 // column modulo 3 00 03 06 10 13 16 20 23 26 // row modulo 3 00 01 02 30 31 32 60 61 62 // row and column modulo 3 00 03 06 30 33 36 60 63 66
00 01 02 03 04 05 06 07 08
10 11 12 13 14 15 16 17 18
20 21 22 23 24 25 26 27 28
30 31 32 33 34 35 36 37 38
40 41 42 43 44 45 46 47 48
50 51 52 53 54 55 56 57 58
60 61 62 63 64 65 66 67 68
70 71 72 73 74 75 76 77 78
80 81 82 83 84 85 86 87 88
I want to split it in this way -
______
00 01 02 |03 04 05 |06 07 08
10 11 12 |13 14 15 |16 17 18
20 21 22 |23 24 25 |26 27 28
30 31 32 |33 34 35 |36 37 38
40 41 42 |43 44 45 |46 47 48
50 51 52 |53 54 55 |56 57 58
60 61 62 |63 64 65 |66 67 68
70 71 72 |73 74 75 |76 77 78
80 81 82 |83 84 85 |86 87 88
Last edited by oceandoe; 11-01-2013 at 05:26 PM.
As per my previous post, simplest way is a couple of nested loops to copy elements to the desired arrays (or array of arrays, in general).
It is definitely possible to use judicious pointer techniques to work with the larger array as if it is a collection of smaller arrays. But that is not exactly the simplest technique, and is error prone unless you know exactly what you are doing and are very careful.
Thank you for your advice!