2. Write a program to find the transpose of a matrix. Matrix is to be stored in a class object which should be constructed using a constructor dynamically by asking rows and columns to the user
Answers
Program in C++
#include<iostream>
using namespace std;
class matrix
{
private:
int r , c;
int a[100][100];
public:
matrix(int rr , int cc)
{
r = rr;
c = cc;
a[r][c];
}
void input()
{
cout<<"Enter matrix : "<<endl;
for(int i = 0 ; i < r ; i++)
{
for(int j = 0 ; j < c ; j++)
{
cin>>a[i][j];
}
}
}
void transpose()
{
for(int i = 0 ; i < c ; i++)
{
for(int j = 0 ; j < r ; j++)
{
cout<<a[j][i]<<"\t";
}
cout<<endl;
}
}
void display()
{
for(int i = 0 ; i < r ; i++)
{
for(int j = 0 ; j < c ; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
};
int main()
{
int r , c;
cout<<"Enter number of rows : ";
cin>>r;
cout<<"Enter number of columns : ";
cin>>c;
matrix m(r , c);
m.input();
cout<<"Original matrix : "<<endl;
m.display();
cout<<"Transposed matrix : "<<endl;
m.transpose();
return 0;
}
Output:
Enter number of rows : 4
Enter number of columns : 3
Enter matrix :
1
2
3
4
5
6
7
8
9
10
11
12
Original matrix :
1 2 3
4 5 6
7 8 9
10 11 12
Transposed matrix :
1 4 7 10
2 5 8 11
3 6 9 12