TutorialsISCClass 11Computer Science

Arrays in Java for ISC Class 11: 1-D Arrays and 2-D Matrix Programs

ISC Class 11 Computer Science notes on arrays in Java: declaring 1-D and 2-D arrays, row-major and column-major address calculation, and solved matrix programs – transpose, boundary sum, diagonal sums and matrix multiplication – for theory and practicals.

13 min read
Arrays in Java for ISC Class 11: 1-D Arrays and 2-D Matrix Programs

Matrix programs appear in almost every ISC practical paper, and array address calculation is a regular theory question. These notes take you from 1-D arrays to the 2-D matrix programs you are most likely to be asked.

1-D arrays: quick recap

int a[] = new int[5];          // 5 elements, indices 0 to 4
int b[] = {3, 8, 1, 9};        // initialised
System.out.println(b.length);  // 4

Searching (linear and binary) and sorting (bubble and selection) on 1-D arrays are covered in our ICSE Class 10 Arrays notes, which are the same algorithms used in ISC.

2-D arrays (matrices)

A 2-D array stores data in rows and columns.

int m[][] = new int[3][4];      // 3 rows, 4 columns
int r = m.length;               // number of rows: 3
int c = m[0].length;            // number of columns: 4

Input and display:

for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
        m[i][j] = sc.nextInt();

for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
        System.out.print(m[i][j] + "\t");
    System.out.println();
}

Address calculation

Memory is linear, so a matrix is stored either row by row (row-major) or column by column (column-major). For an array A[Lr…Ur][Lc…Uc], with base address B and W bytes per element, R rows and C columns:

  • Row-major: Address of A[I][J] = B + W × [(I − Lr) × C + (J − Lc)]
  • Column-major: Address of A[I][J] = B + W × [(J − Lc) × R + (I − Lr)]

Example: A[10][15] is stored in row-major order with B = 2000, W = 4 and both lower bounds 0. Find A[4][6].

= 2000 + 4 × [(4 × 15) + 6] = 2000 + 4 × 66 = 2264

Solved program 1: transpose of a matrix

The transpose swaps rows and columns: T[j][i] = A[i][j].

int t[][] = new int[c][r];
for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
        t[j][i] = m[i][j];

Solved program 2: sum of the boundary elements

A boundary element is in the first or last row, or in the first or last column.

int sum = 0;
for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
        if (i == 0 || i == r - 1 || j == 0 || j == c - 1)
            sum += m[i][j];

For the matrix {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, the boundary sum is 45 − 5 = 40: every element except the centre.

Solved program 3: diagonal sums of a square matrix

int left = 0, right = 0;
for (int i = 0; i < n; i++) {
    left += m[i][i];             // left (main) diagonal
    right += m[i][n - 1 - i];    // right diagonal
}

For the same 3 × 3 matrix: left = 1 + 5 + 9 = 15, and right = 3 + 5 + 7 = 15.

Solved program 4: matrix multiplication

Two matrices A (m × n) and B (p × q) can be multiplied only if n = p. The result is m × q.

import java.util.Scanner;

class MatMul {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Rows and columns of A: ");
        int m = sc.nextInt(), n = sc.nextInt();
        System.out.print("Rows and columns of B: ");
        int p = sc.nextInt(), q = sc.nextInt();
        if (n != p) {
            System.out.println("Multiplication not possible");
            return;
        }
        int a[][] = new int[m][n], b[][] = new int[p][q], c[][] = new int[m][q];
        System.out.println("Enter matrix A:");
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = sc.nextInt();
        System.out.println("Enter matrix B:");
        for (int i = 0; i < p; i++)
            for (int j = 0; j < q; j++)
                b[i][j] = sc.nextInt();

        for (int i = 0; i < m; i++)
            for (int j = 0; j < q; j++)
                for (int k = 0; k < n; k++)
                    c[i][j] += a[i][k] * b[k][j];

        System.out.println("Product:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < q; j++)
                System.out.print(c[i][j] + "\t");
            System.out.println();
        }
    }
}

Example: A = {{1, 2}, {3, 4}} and B = {{5, 6}, {7, 8}} give the product {{19, 22}, {43, 50}}.

Practical exam tips

  1. Validate the size first. ISC questions give limits such as 2 < M < 10, and expect "OUT OF RANGE" or "INVALID INPUT" when the size is outside them.
  2. Print the original matrix before the result. It is usually part of the expected output.
  3. Use \t between elements so the columns line up.
  4. Test with a non-square matrix if the question allows one. Many bugs come from mixing up r and c.

Continue with our ISC Class 12 Computer Science notes and practise matrix questions in the ISC Practical 2027 series.