Home > Back-end >  how to do i copy the values of 1d array to 2d?
how to do i copy the values of 1d array to 2d?

Time:03-16

how do I also display the values of the 1d array to 2d?

Target output:

1 Dimensional arrays
[0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5
[5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 10
2 Dimensional arrays
[0][0]=1 [0][1]=2 [0][2]=3 [0][3]=4 [0][4]=5
[1][0]=6 [1][1]=7 [1][2]=8 [1][3]=9 [1][4]=10

current output:

1 Dimensional arrays
[0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5
[5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 10

[0][0]=4202880 [0][1]=0 [0][2]=37 [0][3]=0 [0][4]=7476048
[1][0]=0 [1][1]=1 [1][2]=0 [1][3]=-1 [1][4]=-1

Code:

#include <stdio.h>

#define row 2
#define col 5

int main()
{
    int one[10];
    int two[row][col];
    int i,j;

    printf("Enter array elements: \n");

    for (i = 0; i < 10; i  ) {
        scanf("%d",&one[i]);
    }

    printf("1 Dimensional arrays  \n");

    for (i = 0; i < 5; i  ) {
        printf("[%d] = %d\t", i, one[i]);
    }

    printf("\n");

    for (i = 5; i < 10; i  ) {
        printf("[%d] = %d\t", i, one[i]);
    }

    printf("\n\n");

    /* 2 dimensional*/
    for (i = 0; i < 2; i  ) {
        for (j = 0; j < 5; j  )
            one[10] = two[row][col];
    }

    for (i = 0; i < 2; i  ) {
        for (j = 0; j < 5; j  )
            printf("[%d][%d]=%d ", i, j, two[i][j]);
        printf("\n");
    }
}

CodePudding user response:

Use a third variable to index the one dimensional array as you iterate through the 2D indices.

#include <stdio.h>

#define ROWS 2
#define COLS 5

int main(void) {
    unsigned one_dim[ROWS * COLS] = {
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    };

    unsigned two_dim[ROWS][COLS];

    for (size_t i = 0, k = 0; i < ROWS; i  )
        for (size_t j = 0; j < COLS; j  )
            two_dim[i][j] = one_dim[k  ];

    for (size_t i = 0; i < ROWS; i  ) {
        for (size_t j = 0; j < COLS; j  )
            printf("[%zu][%zu]=%u ", i, j, two_dim[i][j]);

        putchar('\n');
    }
}

stdout:

[0][0]=1 [0][1]=2 [0][2]=3 [0][3]=4 [0][4]=5
[1][0]=6 [1][1]=7 [1][2]=8 [1][3]=9 [1][4]=10

CodePudding user response:

Modified the second loop.

#include <stdio.h>
#define row 2
#define col 5
int main() 
{
int one[10];
int two[row][col];
int i, j;
printf("Enter array elements: \n");
for (i = 0; i < 10; i  ) {
    scanf("%d", &one[i]);
}
printf("1 Dimensional arrays  \n");

for (i = 0; i < 5; i  ) {
    printf("[%d] = %d\t", i, one[i]);
}
printf("\n");
for (i = 5; i < 10; i  ) {
    printf("[%d] = %d\t", i, one[i]);
}
printf("\n\n");
/* 2 dimensional*/
int cnt = 0;
for (i = 0; i < 2; i  ) {
    for (j = 0; j < 5; j  ) {
        two[i][j] = one[cnt];
        cnt = cnt   1;
    }
}
for (i = 0; i < 2; i  ) {
    for (j = 0; j < 5; j  )
        printf("[%d][%d]=%d ", i, j, two[i][j]);
    printf("\n");
}
}

CodePudding user response:

Not sure where you are stuck, what misbehaviour you observe.
But this one[10] = two[row][col]; is very likely better
one[i*5 j] = two[i][j]; or
one[i j*2] = two[i][j]; for whatever you are trying.
Or reverse,
two[i][j]=one[i*5 j]; or
two[i][j]=one[i j*2];.

This calculates, while iterating through one array, the index/indexes for the other array from the loop counters. It also solves most (maybe all) issues which are mentioned in the comments.

  • Related