Newbiehere - 10 months ago 54

C++ Question

This is what I have so far.

`#include <stdio.h>`

void transposeMatrix(int A2[4][5],int A1[5][4])

{

int i, j;

for (i = 0; i < 5; ++i){

for (j = 0; j < 4; ++j)

A1[j][i]=A2[i][j];

}

}

int main(void)

{

int A2[4][5] = {

{ 7, 4, 2, 1, 12 },

{ 4, 6, 22, 11, 6 },

{ 12, 10, 3, 1, 2 },

{ 20, 4, 1, 3, 4 },

};

int A1[5][4];

int i, j;

printf("Original matrix:\n ");

for (i = 0; i < 4; ++i){

for (j = 0; j < 5; ++j)

printf("%5i", A2[i][j]);

printf("\n");

}

printf("Transposed matrix:\n");

transposeMatrix(A2, A1);

for (i = 0; i < 5; ++i){

for (j = 0; j < 4; ++j)

printf("%5i", A1[i][j]);

printf("\n");

}

return 0;

}

I am trying to transpoes 4 by 5 matrix to 5 by 4 matrix. When I debug, this is what I get. It only transposes first row. What should I do from now?

[![enter image description here][1]][1][enter image description here][2]

http://i.stack.imgur.com/sww1d.png

Answer Source

You have one error in your code.

It is an index problem in the `transpose`

function.

```
for (i = 0; i < 5; ++i){
for (j = 0; j < 4; ++j)
A1[i][j]=A2[j][i];
}
```

and not `A1[j][i]=A2[i][j]`

.