Thread: to convert two-dimensional array to one-dimensional

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    4

    to convert two-dimensional array to one-dimensional

    (C programming) Guys, please help me convert two-dimensionalarray to one-dimensional in such way:
    --->---
    |
    ---<---
    |
    --->---
    |
    ---<---
    Last edited by fsbf; 4 Weeks Ago at 08:10 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,284
    So you want to flatten
    1 2 3
    4 5 6
    7 8 9


    into
    1 2 3 6 5 4 7 8 9
    ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    38
    2d arrays use continous memory, so you can just declare a 1d array and use memcpy.

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    4

    Yes/

    you`re right
    Quote Originally Posted by Salem View Post
    So you want to flatten
    1 2 3
    4 5 6
    7 8 9


    into
    1 2 3 6 5 4 7 8 9
    ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,284
    Do you want to make a copy of the data?
    Code:
    int from[3][3];
    int to[9];
    // some code to copy from -> to
    Or you do want to modify the array in place, then pretend it's a 1D array by pointing to the start of it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    121
    Quote Originally Posted by Salem View Post
    Or you do want to modify the array in place, then pretend it's a 1D array by pointing to the start of it?
    Interested in why you say "pretend"... I don't think like there there is any 'pretend' in there!

    (ie. In my mind, when I cast something to a new data type, it is that datatype).

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    11
    Quote Originally Posted by thmm View Post

    2d arrays use continous memory, so you can just declare a 1d array and use memcpy.
    Interesting and good to know.

    My simple, probably newbie-ish and uncreative algorithm would be:


    • Create new one dimensional array with the capacity equals to the capacity of the old array.
    • Transverse over old array picking up the current item while at the same time transversing over the new array adding the current item


    There, you have the old multidimensional array and new one dimensional array.

    Guessing now you can discard or destory or deallocate the old array as its contents are now in the new one dimensional array.

    profit?!?!?!
    Last edited by ghoul; 4 Weeks Ago at 12:35 AM.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    455
    Quote Originally Posted by fsbf View Post
    (C programming) Guys, please help me convert two-dimensionalarray to one-dimensional in such way:
    --->---
    |
    ---<---
    |
    --->---
    |
    ---<---
    MUltidimensional arrays don't work well in C. A lot of data structures are naturally two diemensional, but their dimensions are not known at compile time. Eg images.

    The normal thing to do is to create the buffer with

    Code:
    Pixel *image = malloc(width * height * sizeof(PIxel));
    we then access the pixels like this

    Code:
    image[y*width+x] = BLACK;
    If you have a 2D array for some reason and you want to treat it as a 1D array, yu don't ned to do anything except cast

    Code:
     double matrix[4][4]; // 2d matrix
     double *mtxptr = (double *) matrix; // 16 element 1 d array using the same memory buffer
    
    mtxptr[15] = 0.0; // set the last element to 0.0
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,284
    Some fun for a lazy Saturday
    Code:
    #include<stdio.h>
    
    #define ASIZE 5
    
    void zigzag(int a[ASIZE][ASIZE]) {
      int flip = 0;
      for( int r = 0 ; r < ASIZE ; r++ ) {
        for( int c = 0 ; c < ASIZE ; c++ ) {
          int idx = (0+c)*(1-flip) + ((ASIZE-1)-c)*(flip);
          printf("%2d ", a[r][idx]);
        }
        flip = !flip;
        printf("\n");
      }
    }
    
    int main() {
      int a[ASIZE][ASIZE] = {
        { 1, 2, 3, 4, 5 },
        { 5, 4, 3, 2, 1 },
        { 11, 12, 13, 14, 15 },
        { 15, 14, 13, 12, 11 },
        { 21, 22, 23, 24, 25 },
      };
      zigzag(a);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    62
    Hallo!

    I think, he can use union to make it easier:
    Code:
    #include <stdio.h>
    
    void initArray(int (*array)[], int n, int init)
    {
     for (int slei = 0; slei < n; slei++)
      (*array)[slei] = init++;
    }
    
    
    int main(int argc, char **argv)
    {
    int x, y, wert, slei;
    
    union Kumu
     {
     int a[5][5];  // 25 = 5 * 5 =a[rows][cols]
     int satz[25]; // 25 = 5rows * 5cols
    }km;
    wert = 4;
    x = y = 1;
    
    printf("\n2dim to 1 dim\n");
    
    initArray(&km.satz, 25, 1);
    
    for (y = 0; y < 5; y++)
     {
      for (x = 0; x < 5; x++)
       printf("\nkm.a[%d][%d]=%d", y, x, km.a[y][x]);
      printf("\n-----------------\n");
     }
     
      return 0;
    }
    to convert two-dimensional array to one-dimensional-2dim_to_1dim-jpg

  11. #11
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    62
    One more thing:

    If you want to turn axis like

    1 2 3 4 5 6 7 8 9

    from

    1 2 3
    4 5 6
    7 8 9


    to

    147
    258
    369

    below there is an other version of my example.

    You can use such way to encode very simple a Letter:

    Hello Johny there.....

    Hello
    Johny
    there
    to

    H J t
    e o h
    l h e
    l n r
    o y e

    Later, in a file you can read

    H J t e o h l h e l n r o y e


    here the second example:
    Code:
    #include <stdio.h>
    
    void initArray(int (*array)[], int n, int init)
    {
     for (int slei = 0; slei < n; slei++)
      (*array)[slei] = init++;
    }
    
    void ShowMatrix(int a[15][8])
    {
     int x, y;   
     
     for (y = 0; y < 15; y+=3)
     {
      for (x = 0; x < 8; x++)
       {
        printf("\nkm.a[%02d][%02d]=%3d", y, x, a[y][x]);
        printf("     km.a[%02d][%02d]=%3d", y + 1, x, a[y + 1][x]);
        printf("     km.a[%02d][%02d]=%3d", y + 2, x, a[y + 2][x]);
       }
      
      printf("\n-----------------\n");
     }
    }
    
    void ShowSatz(int satz[120], int n)
    {
     int x;   
     
      printf("\n--------Satz-----------\n");
     
       for (x = 0; x < n; x+=4)
       printf("\nSatz[%03d]=%3d    %03d=%3d      %03d=%3d       %03d=%3d", x, satz[x], x + 1, satz[x +1], x + 2, satz[x +2], x + 3, satz[x +3]);
      
    }
    
    void ShowMatrixZeilenweise(int a[15][8])
    {
     int x, y;   
     
     for (x = 0; x < 8; x+=2)
     {
      for (y = 0; y < 15; y++)
       {
        printf("\nkm.a[%03d][%03d]=%3d", y, x, a[y][x]);
        printf("   km.a[%03d][%03d]=%3d", y, x + 1, a[y][x + 1]);
       }
      printf("\n-----------------\n");
     }
    }
    
    
    int main()
    {
    int x, y, k, wert, slei, a[15][8], satz[120];
    wert = 4;
    
    printf("\nMatrix_ur V0.02\n");
    
    initArray(&satz, 120, 1);
    ShowSatz(satz, 120);
    
    printf("\n################################################################\n");
    
     k = 0;
    
    
    // Init Matrix 
    for (y = 0; y < 15; y++)
     {
      for (x = 0; x < 8; x++)
        a[y][x]= satz[k++];
     }
     
    
     ShowMatrix(a);
    
    printf("\n################################################################\n");
     k = 0;
    
    // Init Matrix 
    for (x = 0; x < 8; x++)
     {
      for (y = 0; y < 15; y++)
        a[y][x]= satz[k++];
     }
     
    
     ShowMatrixZeilenweise(a);
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-19-2017, 04:46 AM
  2. Assigning One dimensional array to two dimensional array
    By lightning_star in forum C Programming
    Replies: 1
    Last Post: 03-19-2014, 09:44 PM
  3. Replies: 12
    Last Post: 09-02-2013, 07:50 PM
  4. Replies: 4
    Last Post: 09-02-2013, 11:19 AM
  5. Replies: 24
    Last Post: 11-11-2008, 01:39 PM

Tags for this Thread