(C programming) Guys, please help me convert two-dimensionalarray to one-dimensional in such way:
--->---
ᅠᅠᅠᅠ|
---<---
|
--->---
ᅠᅠᅠᅠ|
---<---
(C programming) Guys, please help me convert two-dimensionalarray to one-dimensional in such way:
--->---
ᅠᅠᅠᅠ|
---<---
|
--->---
ᅠᅠᅠᅠ|
---<---
Last edited by fsbf; 10-22-2020 at 08:10 AM.
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.
2d arrays use continous memory, so you can just declare a 1d array and use memcpy.
Do you want to make a copy of the data?
Or you do want to modify the array in place, then pretend it's a 1D array by pointing to the start of it?Code:int from[3][3]; int to[9]; // some code to copy from -> to
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.
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; 10-23-2020 at 12:35 AM.
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
we then access the pixels like thisCode:Pixel *image = malloc(width * height * sizeof(PIxel));
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 castCode:image[y*width+x] = BLACK;
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
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.
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; }
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; }