Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. main creates an object ob of the class Transpose. The constructor sets m and n to 0.
  2. main calls ob.accept():
    • it reads m and n with a Scanner;
    • if either is less than 1 or more than 10 it returns false;
    • otherwise it creates the arrays a[m][n] and t[n][m], reads the elements of a row by row with two nested loops and returns true.
  3. If accept() returned false, main prints the invalid size message and stops.
  4. Otherwise main calls ob.findTranspose(), which for every i from 0 to m - 1 and every j from 0 to n - 1 copies a[i][j] into t[j][i].
  5. main calls ob.display(), which prints the original matrix and then the transpose by calling show() for each. The method show(x, r, c) prints the first r rows and c columns of the array x, 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

VariableData TypePurpose
aint[][]Data member that stores the original matrix
tint[][]Data member that stores the transpose
mintData member for the number of rows of the matrix
nintData member for the number of columns of the matrix
scScannerReads the size and the elements typed by the user
xint[][]Parameter of show(): the array to be printed
rintParameter of show(): number of rows to print
cintParameter of show(): number of columns to print
iintLoop counter for the row index
jintLoop counter for the column index
obTransposeObject of the class created in main
argsString[]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.

One value per line

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:

ija[i][j]Statementt after the step
001t[0][0] = 1row 0: 1, -
012t[1][0] = 2row 1: 2, -
023t[2][0] = 3row 2: 3, -
104t[0][1] = 4row 0: 1, 4
115t[1][1] = 5row 1: 2, 5
126t[2][1] = 6row 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

  1. Symmetric matrix "Write a program to check whether a square matrix is symmetric, that is, equal to its transpose."
  2. Transpose in place "Transpose a square matrix without using a second array, by swapping a[i][j] and a[j][i]."