2. A Transpose of an array is obtained by interchanging the elements of the rows and columns. A class Transarray contains a two dimensional integer array of order [m x n]. The maximum value possible for both `m’ and `n’ is 20. Design a class Transarray to find the transpose of a given matrix. The details of the members of the class are given below Class name : Transarray Data members/instance variables : arr[] : stores the matrix elements M : integer to store the number of rows N : integer to store the number of columns Member functions : Transarray() : default constructor Transarray(int mm, int nn) : to initialize the size of the matrix, m=mm, n=nn void fillarray() : to enter the elements of the matrix void transpose(Transarray A) : to find the transpose of a given matrix void disparrary() : displays the array in a matrix form Specify the class Transarray giving the details of the constructors, void fillarray(), void transpose(Transarray) and void disparray(). You need not write the main function.
Answers
Answer:
import java.util.*;
class Transarray
{
int arr[][],m,n;
Transarray()
{
m=0;
n=0;
arr=new int[m][n];
}
Transarray(int mm,int nn)
{
m=mm;
n=nn;
arr=new int[m][n];
}
void fillarray()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE ARRAY ELEMENTS");
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
void transpose(Transarray A)
{
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
arr[j][i]=A.arr[i][j];
}
}
}
void disaparray()
{
for (int i=0;i<arr.length;i++)
{
for (int j=0;j<((m*n)/arr.length);j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println("");
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
Transarray o=new Transarray();
System.out.println("Enter the number of rows");
int mm=sc.nextInt();
System.out.println("Enter the number of columns");
int nn=sc.nextInt();
Transarray ob1=new Transarray();
Transarray A=new Transarray();
ob1.fillarray();
System.out.println("THE ORIGINAL MATRIX::");
ob1.disaparray();
Transarray ob2=new Transarray();
ob2.transpose(ob1);
System.out.println("THE TRANSPOSE MATRIX::");
ob2.disaparray();
}
}
Explanation:
Hopefully this will help you.