0% found this document useful (0 votes)
17 views6 pages

Q2

Uploaded by

Mehul Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Q2

Uploaded by

Mehul Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Objective: 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.

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.

You might also like