A2 Ques1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

/*Q1.

Give a C++ code function that, given a n × n matrix M of type


float, replaces M with its transpose without the use of a temporary
matrix.*/
#include <iostream>
using namespace std;
class Matrix
{
int **a;
int row ,col;
public:
Matrix(int x=0,int y=0)
{
row=x;
col=y;
a = new int *[row];
for (int i=0;i<row;i++)
{
a[i]=new int [col];
}
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
a[i][j]=0;
}
}
}
void getvalue()
{
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
cout<<"Enter Value for A["<<i<<"]["<<j<<"] : ";
cin>>a[i][j];
}
}
}
Matrix transpose() //transpose function
{

for (int i=1;i<row;i++)


{
for (int j=0;j<col;j++)
{
int temp=a[j][i];
a[j][i]=a[i][j];
a[i][j]=temp;
}
}
}
void display()
{
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
int r1,c1;
cout<<"For Matrix 1:"<<endl<<"Enter No. of Rows:";
cin>>r1;
cout<<"Enter No. of Columns:";
cin>>c1;
Matrix mat1(r1,c1);
mat1.getvalue();
cout<<"Matrix Formed:"<<endl;
mat1.display();
mat1.transpose();
cout<<endl<<"Transpose of Given matrix is :"<<endl;
mat1.display();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy