برنامه ی ترانسپوزه یا ترانهاده کردن یک ماتریس به زبان سی پلاس پلاس c++
جمعه, ۱۶ مهر ۱۳۹۵، ۰۴:۴۸ ق.ظ
gameover.blog.ir
Transpose Matrix یا ماتریس ترانهاده به ماتریسی می گیم که جای سطر و ستون ماتریس اول رو عوض کنیم.
مثل شکل های زیر:
#include <iostream>
using namespace std;
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;
/* Storing element of matrix entered by user in array a[][]. */
cout << endl << "Enter elements of matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout << "Enter elements a" << i+1 << j+1 << ": ";
cin >> a[i][j];
}
/* Displaying the matrix a[][] */
cout << endl << "Entered Matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout << " " << a[i][j];
if(j==c-1)
cout << endl << endl;
}
/* Finding transpose of matrix a[][] and storing it in array trans[][]. */
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j];
}
/* Displaying the transpose,i.e, Displaying array trans[][]. */
cout << endl << "Transpose of Matrix: " << endl;
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
cout << " " << trans[i][j];
if(j==r-1)
cout << endl << endl;
}
return 0;
}
Output
Enter rows and column of matrix: 2 3 Enter elements of matrix: Enter elements a11: 1 Enter elements a12: 2 Enter elements a13: 9 Enter elements a21: 0 Enter elements a22: 4 Enter elements a23: 7 Entered Matrix: 1 2 9 0 4 7 Transpose of Matrix: 1 0 2 4 9 7
۹۵/۰۷/۱۶