karbaev

matrix-transpose

Mar 8th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a[10][10], trans[10][10], r, c, i, j;
  6.     cout << "Enter rows and columns of matrix: ";
  7.     cin >> r >> c;
  8.  
  9. /* Storing element of matrix entered by user in array a[][]. */
  10.     cout << endl << "Enter elements of matrix: " << endl;
  11.     for(i=0; i<r; ++i)
  12.     for(j=0; j<c; ++j)
  13.     {
  14.         cout << "Enter elements a" << i+1 << j+1 << ": ";
  15.         cin >> a[i][j];
  16.     }
  17. /* Displaying the matrix a[][] */
  18.     cout << endl << "Entered Matrix: " << endl;
  19.     for(i=0; i<r; ++i)
  20.     for(j=0; j<c; ++j)
  21.     {
  22.         cout << " " << a[i][j];
  23.         if(j==c-1)
  24.             cout << endl << endl;
  25.     }
  26.  
  27. /* Finding transpose of matrix a[][] and storing it in array trans[][]. */
  28.     for(i=0; i<r; ++i)
  29.     for(j=0; j<c; ++j)
  30.     {
  31.        trans[j][i]=a[i][j];
  32.     }
  33.  
  34. /* Displaying the transpose,i.e, Displaying array trans[][]. */
  35.     cout << endl << "Transpose of Matrix: " << endl;
  36.     for(i=0; i<c; ++i)
  37.     for(j=0; j<r; ++j)
  38.     {
  39.         cout << " " << trans[i][j];
  40.         if(j==r-1)
  41.             cout << endl << endl;
  42.     }
  43.     return 0;
  44. }
Add Comment
Please, Sign In to add comment