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

1. ## to convert two-dimensional array to one-dimensional

--->---
|
---<---
|
--->---
|
---<--- 2. So you want to flatten
1 2 3
4 5 6
7 8 9

into
1 2 3 6 5 4 7 8 9
? 3. 2d arrays use continous memory, so you can just declare a 1d array and use memcpy. 4. ## Yes/

you`re right Originally Posted by Salem 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. Do you want to make a copy of the data?
Code:
```int from;
int to;
// 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? 6. Originally Posted by Salem 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. Originally Posted by thmm 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?!?!?! 8. Originally Posted by fsbf --->---
|
---<---
|
--->---
|
---<---
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; // 2d matrix
double *mtxptr = (double *) matrix; // 16 element 1 d array using the same memory buffer

mtxptr = 0.0; // set the last element to 0.0``` 9. 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);
}``` 10. 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;  // 25 = 5 * 5 =a[rows][cols]
int satz; // 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;
}```  11. 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)
{
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, 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)
{
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, satz;
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 array, convert, one-dimensional, two-dimensional, two-dimensionalarray 