Reputation: 193
I have two arrays as following .I am using C language
int array1[7][8];
int array2[8][7];
all elemnts of array 1 have some values
I want to assign all values of array 1 to array 2 . Because both arrays have total 56 elements.Values should be able to fit.I want to assign all 56 values of array 1 to array 2 such that if they are seen as 1 dimensional array then they should look identical I tried to iterate through a loop from 0 to 56 and tried to relate their indexes but was unable to figure out. I tried something like this but i am mistaking somewhere
for (i = 0 ; i < 56 ; i ++)
{
array2[i / 7 ][ i % 7 ] = array1[ i / 6 ][ i % 6];
}
Upvotes: 1
Views: 118
Reputation: 25752
Use two loops:
for ( size_t i = 0 ; i < 7 ; i++)
{
for ( size_t j = 0 ; j < 8 ; j++)
{
array2[j][i] = array1[i][j];
}
}
And with a single loop:
for ( size_t i = 0 ; i < 56 ; i++ )
{
array2[i / 7 ][ i % 7 ] = array1[ i / 8 ][ i % 8 ] ;
}
Upvotes: 2
Reputation: 70893
It shall be
for (i = 0; i < 7*8; ++i)
{
array2[i / 7][i % 7] = array1[i / 8][i % 8];
}
Upvotes: 1
Reputation: 108968
This is not a transposition of the array elements!
Ignore the dimensions and memmove()
the data around.
memmove(array2, array1, sizeof array1);
memmove()
doesn't care about the layout of the array elements and the Standard guarantees all array elements (even of multi-dimensional arrays) are sequential.
Upvotes: 2