Anonymous user
Matrix transposition: Difference between revisions
→{{header|C}}: proper casting; improve tag
(Add Seed7 example) |
(→{{header|C}}: proper casting; improve tag) |
||
Line 204:
=={{header|C}}==
Transpose a 2D double array.
<lang c>
void transpose(void *dest, void
{
int i, j;
double (*d)[src_h] = dest, (*s)[src_w] = src;
for(i = 0; i < rows; i++)▼
for (i = 0; i < src_h; i++)
for
d[j][i] = s[i][j];
▲ }
▲<lang c>int main()
{
transpose(b, a, 3, 5);
▲ double b[5][4];
printf("%g%c", b[i][j], j == 2 ? '\n' : ' ');
▲ for(j=0;j<4;j++)
return 0;
▲ for(i=0;i<5;i++)
}</lang>
{{improve|C|Unnecessarily complicated, probably lost all benefit from the algorithm. Can use cleaner coding (and malloc(item_size) does not violate O(1))}}
Playing more to C's strengths, the following implementation transposes a matrix of any type and dimensions
in place with only O(1) space. See the [[wp:In-place_matrix_transposition|Wikipedia article]] for more information.
|