Transpose of a matrix
-
Supose two matrices are there a[3][3] and b[3][3] we scanned the element in a[][] matrix and b is the transpose of former . NOW when we write a code to assign the values we use for(i=0;i<3;i++) {for(j=0;j<3;j++) b[i][j]=a[j][i]; }//correct but when we do b[j][i]=a[i][j] it gives a wrong result why? since in both the cases all assigning cases are same.
-
Supose two matrices are there a[3][3] and b[3][3] we scanned the element in a[][] matrix and b is the transpose of former . NOW when we write a code to assign the values we use for(i=0;i<3;i++) {for(j=0;j<3;j++) b[i][j]=a[j][i]; }//correct but when we do b[j][i]=a[i][j] it gives a wrong result why? since in both the cases all assigning cases are same.
-
Supose two matrices are there a[3][3] and b[3][3] we scanned the element in a[][] matrix and b is the transpose of former . NOW when we write a code to assign the values we use for(i=0;i<3;i++) {for(j=0;j<3;j++) b[i][j]=a[j][i]; }//correct but when we do b[j][i]=a[i][j] it gives a wrong result why? since in both the cases all assigning cases are same.
Of course the result must be the same. Try
#include using namespace std;
void transp_v1( char tgt[3][3], char src[3][3] );
void transp_v2( char tgt[3][3], char src[3][3] );
void dump( char m[3][3] );int main()
{
char a[3][3] =
{
{ 'a', 'b', 'c' },
{ 'd', 'e', 'f' },
{ 'g', 'h', 'i' },
};char b[3][3];
char c[3][3];transp_v1( b, a);
transp_v2( c, a);dump(a);
cout << "--------\n";
dump(b);
cout << "--------\n";
dump(c);
cout << "--------\n";
}
void transp_v1( char tgt[3][3], char src[3][3] )
{
for (int i=0; i<3; ++i)
for (int j=0; j<3; ++j)
tgt[i][j] = src[j][i];
}void transp_v2( char tgt[3][3], char src[3][3] )
{
for (int i=0; i<3; ++i)
for (int j=0; j<3; ++j)
tgt[j][i] = src[i][j];
}void dump( char m[3][3] )
{
for (int i=0; i<3; ++i)
{
for (int j=0; j<3; ++j)
cout << m[i][j] << " ";
cout << "\n";
}
}output:
a b c
d e f
g h ia d g
b e h
c f ia d g
b e h
c f i -
Of course the result must be the same. Try
#include using namespace std;
void transp_v1( char tgt[3][3], char src[3][3] );
void transp_v2( char tgt[3][3], char src[3][3] );
void dump( char m[3][3] );int main()
{
char a[3][3] =
{
{ 'a', 'b', 'c' },
{ 'd', 'e', 'f' },
{ 'g', 'h', 'i' },
};char b[3][3];
char c[3][3];transp_v1( b, a);
transp_v2( c, a);dump(a);
cout << "--------\n";
dump(b);
cout << "--------\n";
dump(c);
cout << "--------\n";
}
void transp_v1( char tgt[3][3], char src[3][3] )
{
for (int i=0; i<3; ++i)
for (int j=0; j<3; ++j)
tgt[i][j] = src[j][i];
}void transp_v2( char tgt[3][3], char src[3][3] )
{
for (int i=0; i<3; ++i)
for (int j=0; j<3; ++j)
tgt[j][i] = src[i][j];
}void dump( char m[3][3] )
{
for (int i=0; i<3; ++i)
{
for (int j=0; j<3; ++j)
cout << m[i][j] << " ";
cout << "\n";
}
}output:
a b c
d e f
g h ia d g
b e h
c f ia d g
b e h
c f ibut as per my code its giving wrong result whats wrong with my code i tested many times void main(){ int a[3][3],b[3][3],i,j; for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++) {for(j=0;j<3;j++) {printf("%d ",a[i][j]); } printf("\n"); } for(i=0;i<3;i++) {for(j=0;j<3;j++) {b[j][i]=a[i][j]; printf("%d ",b[i][j]); } printf("\n"); } }
-
but as per my code its giving wrong result whats wrong with my code i tested many times void main(){ int a[3][3],b[3][3],i,j; for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++) {for(j=0;j<3;j++) {printf("%d ",a[i][j]); } printf("\n"); } for(i=0;i<3;i++) {for(j=0;j<3;j++) {b[j][i]=a[i][j]; printf("%d ",b[i][j]); } printf("\n"); } }
Your code is wrong because you are printing the
b
matrix while you're updating it (you are printing 'not yet updated' items). Try#include int main()
{
int a[3][3],b[3][3],i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[j][i]=a[i][j];
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
return 0;
} -
but as per my code its giving wrong result whats wrong with my code i tested many times void main(){ int a[3][3],b[3][3],i,j; for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++) {for(j=0;j<3;j++) {printf("%d ",a[i][j]); } printf("\n"); } for(i=0;i<3;i++) {for(j=0;j<3;j++) {b[j][i]=a[i][j]; printf("%d ",b[i][j]); } printf("\n"); } }
b[j][i]=a[i][j];
printf("%d ",b[i][j]);So you wanted to print the value of b[i][j] after you updated b[j][i]?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle