program to find the transpose of matrix using class


Write a program to create a ‘Matrix’ class and find the transpose of matrix.

SOURCE CODE

#include<iostream>
using namespace std;
class MATRIX
{
int a[5][5];
int row,col;
public: MATRIX(int x,int y)
{
row=x;
col=y;
}
void read();
void display();
void transpose();
};
void MATRIX::read()
{
cout<<“Enter elements of matrix:\n”;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++) cin>>a[i][j];
}
}
void MATRIX::display()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<a[i][j]<<” “;
cout<<“\n”;
}
}
void MATRIX::transpose()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<a[j][i]<<” “;
cout<<“\n”;
}
}
main()
{
int m,n;
cout<<“Enter order of matrix:”; cin>>m>>n;
MATRIX obj1(m,n);
obj1.read();
cout<<“Matrix is…\n”;
obj1.display();
cout<<“Transpose of matrix is…\n”;
obj1.transpose();
}

OUTPUT

Enter order of matrix:3 3
Enter elements of matrix:
1
2
3
4
5
6
7
8
9
Matrix is…
1 2 3
4 5 6
7 8 9
Transpose of matrix is…
1 4 7
2 5 8
3 6 9

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s