how to properly convery 1d array to 2d array?

I am having trouble converting 2d array to 1d array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
given a 1d array
 unsigned char 1d[] = {
        0x01, 0x02, 0x03, 0x04, 0x05,
        0x06, 0x07, 0x08, 0x09, 0x00,
        0x01, 0x02, 0x03, 0x04, 0x05,
        0x06, 0x07, 0x08, 0x09, 0x00
    };

//I am trying to convert that 1d array to a 2d array. I printf width and height. 
width = 5, height = 4. 

for (int i = 0; i < height; i++){
	   for (int j = 0; j < width; j++){
		printf("%d",1d[icounter]);
		iarray[i][j] = 1d[icounter];
		icounter++;}}

//result: 12345678901324567890 same thing if I do print iarray[i][j].

for (int i = 0; i < height; i++){
	   for (int j = 0; j < width; j++){
		printf("%d",iarray[i][j]);}}

//result: 123466789112345667890. Why? why 6 and 1 appear twice. To help you guys visualize it I'll have one more for loop exactly the same as the above. but with some space. 

for (int i = 0; i < height; i++){printf("\n");
	   for (int j = 0; j < width; j++){
		printf("%d",iarray[i][j]);}}
/*
result: 
1 2 3 4 6
6 7 8 9 1
1 2 3 4 6 
6 7 8 9 0 */

/*
I want my result to be 
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
6 7 8 9 0 */

//lastly I try printing the incidences to see what's going on. 
for (int i = 0; i < height; i++){
	   for (int j = 0; j < width; j++){
		printf("(%d",i);
		printf(",%d",j);
		printf(")");
		printf("%d\n",iarray[i][j]);}}

/*result: 
(0,0)1
(0,1)2
(0,2)3
(0,3)4
(0,4)6
(1,0)6
(1,1)7
(1,2)8
(1,3)9
(1,4)1
(2,0)1
(2,1)2
(2,2)3
(2,3)4
(2,4)6
(3,0)6
(3,1)7
(3,2)8
(3,3)9
(3,4)0
*/
Now i'm confused.  
Last edited on
Try this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstring>

unsigned char source[] = {
        0x01, 0x02, 0x03, 0x04, 0x05,
        0x06, 0x07, 0x08, 0x09, 0x00,
        0x01, 0x02, 0x03, 0x04, 0x05,
        0x06, 0x07, 0x08, 0x09, 0x00
    };


int main()
{
    const int
            row = 4,
            column = 5;

    printf("1d to 2d\n\n");

    for(int i = 0; i < row; ++i)
    {
        for(int k = 0; k < column; ++k)
        {
            static int index = 0;
            printf("%d",source[index]);
            ++index;
        }
        printf("\n");
    }

    printf("\n1d\n\n");

    for(int i = 0; i < row*column; ++i)
    {
        printf("%d\n",source[i]);
    }

    return 0;
}



Last edited on
^ I don't understand your code
I want to convert an array[m*n] to array[m][n]
That's what it does at the stage "1d to 2d".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <string.h>

int main()
{
    enum { N = 4, M = 5, SZ = N*M };

    const int one_d[SZ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ;

    int two_d[N][M] ;
    memcpy( two_d[0], one_d, SZ * sizeof(int) ) ;

    for( int i = 0 ; i < N ; ++i )
    {
       for( int j = 0 ; j < M ; ++j ) printf( "%d ", two_d[i][j] ) ;
       puts( "" ) ;
    }
}

http://coliru.stacked-crooked.com/a/68d83f34bde96b24
^ thanks, but can anyone explain why my original code doesn't work? It makes perfect sense.

also. The one_d should be unsigned char memcpy doesn't work. Width and height is changable depending on the size of 1d. So it is not possible to do enum;

unsigned char 1d[] = {
0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x00
};
Last edited on
Topic archived. No new replies allowed.