Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Transpose of a matrix

Transpose of a matrix

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14849246
    wrote on last edited by
    #1

    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.

    K CPalliniC 2 Replies Last reply
    0
    • M Member_14849246

      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.

      K Offline
      K Offline
      kalberts
      wrote on last edited by
      #2

      This should be very easy to trace in a debugger. You don't even tell in which way the result is wrong, and you do not show the declaration to fhe two matrices, so there is really not much information to make a qualified guess.

      1 Reply Last reply
      0
      • M Member_14849246

        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.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        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 i

        a d g
        b e h
        c f i

        a d g
        b e h
        c f i

        In testa che avete, signor di Ceprano?

        M 1 Reply Last reply
        0
        • CPalliniC CPallini

          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 i

          a d g
          b e h
          c f i

          a d g
          b e h
          c f i

          M Offline
          M Offline
          Member_14849246
          wrote on last edited by
          #4

          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"); } }

          CPalliniC J 2 Replies Last reply
          0
          • M Member_14849246

            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"); } }

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            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;
            }

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • M Member_14849246

              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"); } }

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups