Splitting 2D array

This is a discussion on Splitting 2D array within the C Programming forums, part of the General Programming Boards category; What is the simpliest way to split 2D array - for example - Split 9x9 2D array to - 3x3 ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    3

    Splitting 2D array

    What is the simpliest way to split 2D array - for example - Split 9x9 2D array to - 3x3 2D arrays (squares).

    Thank you!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,530
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,639
    It depends on exactly how you want to split it. For example, if your array is
    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
    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:
    // "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

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    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 06:26 PM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,530
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    Thank you for your advice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting Hex into an array
    By fletch254 in forum C Programming
    Replies: 13
    Last Post: 01-11-2013, 02:59 AM
  2. Splitting an int array into four arrays
    By zak9000 in forum C Programming
    Replies: 2
    Last Post: 01-02-2012, 06:08 PM
  3. Splitting An Array
    By fault in forum C Programming
    Replies: 1
    Last Post: 02-28-2011, 10:20 PM
  4. Splitting stream of bytes into block of array.
    By gravityzer0 in forum C Programming
    Replies: 7
    Last Post: 08-22-2006, 07:27 PM
  5. Splitting up a char array?
    By stillwell in forum C++ Programming
    Replies: 28
    Last Post: 10-18-2004, 12:56 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21