ISC Practicals and Theory - Transpose (OOP)
Updated 20 Oct 2026
The transpose of a matrix is the matrix whose rows are the columns of the original. This version stores the matrices as data members of the class Transpose, and separate methods read, transpose and display them while main only creates an object and calls the methods.
Definition
The transpose of a matrix $A$ with $m$ rows and $n$ columns is the matrix $A^T$ with $n$ rows and $m$ columns, where the element in row $i$, column $j$ of $A$ becomes the element in row $j$, column $i$ of $A^T$:
t[j][i] = a[i][j]
For example, $A$ with rows $1\ 2\ 3$ and $4\ 5\ 6$ ($2 \times 3$) has the transpose with rows $1\ 4$, $2\ 5$ and $3\ 6$ ($3 \times 2$).
Algorithm
maincreates an objectobof the classTranspose. The constructor setsmandnto 0.maincallsob.accept():- it reads
mandnwith aScanner; - if either is less than 1 or more than 10 it returns
false; - otherwise it creates the arrays
a[m][n]andt[n][m], reads the elements ofarow by row with two nested loops and returnstrue.
- it reads
- If
accept()returnedfalse,mainprints the invalid size message and stops. - Otherwise
maincallsob.findTranspose(), which for everyifrom 0 tom - 1and everyjfrom 0 ton - 1copiesa[i][j]intot[j][i]. maincallsob.display(), which prints the original matrix and then the transpose by callingshow()for each. The methodshow(x, r, c)prints the firstrrows andccolumns of the arrayx, one row per line.
Program
The data members a, t, m and n are shared by all the methods, so only show() needs parameters.
import java.util.Scanner;
public class Transpose
{
int a[][], t[][];
int m, n;
Transpose()
{
m = 0;
n = 0;
}
boolean accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows (m): ");
m = sc.nextInt();
System.out.print("Enter the number of columns (n): ");
n = sc.nextInt();
if (m < 1 || m > 10 || n < 1 || n > 10)
{
return false;
}
a = new int[m][n];
t = new int[n][m];
System.out.println("Enter " + (m * n) + " elements row by row:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}
return true;
}
void findTranspose()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
t[j][i] = a[i][j];
}
}
}
void show(int x[][], int r, int c)
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
System.out.print(x[i][j] + "\t");
}
System.out.println();
}
}
void display()
{
System.out.println("Original matrix (" + m + " x " + n + "):");
show(a, m, n);
System.out.println("Transpose (" + n + " x " + m + "):");
show(t, n, m);
}
public static void main(String args[])
{
Transpose ob = new Transpose();
if (ob.accept())
{
ob.findTranspose();
ob.display();
}
else
{
System.out.println("Invalid size. Rows and columns must be from 1 to 10.");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
a | int[][] | Data member that stores the original matrix |
t | int[][] | Data member that stores the transpose |
m | int | Data member for the number of rows of the matrix |
n | int | Data member for the number of columns of the matrix |
sc | Scanner | Reads the size and the elements typed by the user |
x | int[][] | Parameter of show(): the array to be printed |
r | int | Parameter of show(): number of rows to print |
c | int | Parameter of show(): number of columns to print |
i | int | Loop counter for the row index |
j | int | Loop counter for the column index |
ob | Transpose | Object of the class created in main |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter the number of rows (m): 2
Enter the number of columns (n): 3
Enter 6 elements row by row:
1 2 3
4 5 6
Original matrix (2 x 3):
1 2 3
4 5 6
Transpose (3 x 2):
1 4
2 5
3 6 Sample 2
Enter the number of rows (m): 3
Enter the number of columns (n): 1
Enter 3 elements row by row:
7
8
9
Original matrix (3 x 1):
7
8
9
Transpose (1 x 3):
7 8 9 Sample 3
Enter the number of rows (m): 11
Enter the number of columns (n): 2
Invalid size. Rows and columns must be from 1 to 10.Dry run
Take the input $m = 2$, $n = 3$ with rows $1\ 2\ 3$ and $4\ 5\ 6$. After accept(), a holds these values and t has 3 rows and 2 columns. Each step of findTranspose() is:
| i | j | a[i][j] | Statement | t after the step |
|---|---|---|---|---|
| 0 | 0 | 1 | t[0][0] = 1 | row 0: 1, - |
| 0 | 1 | 2 | t[1][0] = 2 | row 1: 2, - |
| 0 | 2 | 3 | t[2][0] = 3 | row 2: 3, - |
| 1 | 0 | 4 | t[0][1] = 4 | row 0: 1, 4 |
| 1 | 1 | 5 | t[1][1] = 5 | row 1: 2, 5 |
| 1 | 2 | 6 | t[2][1] = 6 | row 2: 3, 6 |
display() then prints a with show(a, 2, 3) and t with show(t, 3, 2), giving the $3 \times 2$ transpose with rows $1\ 4$, $2\ 5$ and $3\ 6$, as in the first sample run.
Variations
- Symmetric matrix "Write a program to check whether a square matrix is symmetric, that is, equal to its transpose."
- Transpose in place "Transpose a square matrix without using a second array, by swapping
a[i][j]anda[j][i]."