Q2
Q2
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.
Source Code:
import java.util.Scanner;
public class Transarray
{
int arr[][], tarr[][]; int M, N;//class variables
Transarray()
{
M=0; N=0;//cannot initialise array without knowing its size
}
Transarray (int mm, int nn)
{
M=mm; N=nn;
arr = new int[M][N];
tarr = new int[N][M];//initialise array after we know its size
}
void fillarray()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the elements of the array");
for(int i=0; i<M; i++)
{
for(int j=0; j<N; j++)
{
arr[i][j]=sc.nextInt();//enters array elements
}
}
}
void transpose(Transarray A)
{
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
A.tarr[i][j]=A.arr[j][i];//transposes the array
}
}
}
void disparray()
{
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
System.out.print(tarr[i][j]+" ");//prints the transposed array
}
System.out.println();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter dimensions of the array");
int m = sc.nextInt();
int n=sc.nextInt();
Transarray A = new Transarray(m,n);
A.fillarray();
A.transpose(A);
A.disparray();
}
}
Algorithm:
1. Class Transarray:
2. Declare arr[][], tarr[][]
3. Declare M, N
4.
5. Method Transarray()
6. Set M = 0, N = 0
7.
8. Method Transarray(int mm, int nn)
9. Set M = mm, N = nn
10. Initialize arr as MxN array
11. Initialize tarr as NxM array
12.
13. Method fillarray()
14. Print "Enter the elements of the array"
15. For i from 0 to M-1
16. For j from 0 to N-1
17. Read arr[i][j]
18.
19. Method transpose(Transarray A)
20. For i from 0 to N-1
21. For j from 0 to M-1
22. A.tarr[i][j] = A.arr[j][i]
23.
24. Method disparray()
25. For i from 0 to N-1
26. For j from 0 to M-1
27. Print tarr[i][j] with a space
28. Print new line
29.
30. Method main(String[] args)
31. Create Transarray object A with dimensions m and n
32. Call A.fillarray()
33. Call A.transpose(A)
34. Call A.disparray()
Output:
VDT:
Variable Name Type Description
arr int[][] 2D array to store the
original matrix
elements.
tarr int[][] 2D array to store the
transposed matrix
elements.
M int Number of rows in the
original matrix.
N int Number of columns in
the original matrix.
sc Scanner Scanner object to read
input from the user.
i int Loop variable used to
iterate over rows.
j int Loop variable used to
iterate over columns.
A Transarray Instance of the
Transarray class used to
access and manipulate
the array and its
transpose.