ISC practical papers › 2027 masterclass › Episode 2
2D Matrices & Boundary Rotations
ISC 2027 Computer Science Practical masterclass · Episode 2 of 4 · Vinay Singh
The problem
Declare a matrix with M rows and N columns, where 2 < M < 10 and 2 < N < 10. Read its positive integer elements and a rotation factor K. Rotate the boundary (the outer ring of cells) clockwise by K positions, keep the inner cells unchanged, and count how many of the four corners of the rotated matrix are prime.
Rules and messages
- If M or N is not between 3 and 9, print INVALID SIZE.
- If an element is not a positive integer, print INVALID ELEMENT: Must be positive integer.
- If K is less than 1 or not less than the boundary length L = 2(M + N) - 4, print INVALID VALUE OF K.
Algorithm
Written step by step, the way the ISC practical asks for it, and matched to the program below. How the algorithm is marked
- Step 1: Start.
- Step 2: Input M and N.
- Step 3: If M or N is less than or equal to 2, or greater than or equal to 10, display INVALID SIZE and stop.
- Step 4: Input positive integer elements into mat[M][N].
- Step 5: Input K. Calculate L = 2 x (M + N) - 4.
- Step 6: If K is less than 1 or greater than or equal to L, display INVALID VALUE OF K and stop.
- Step 7: Display the original matrix.
- Step 8: Copy the boundary clockwise into the array boundary of length L: top row (left to right), right column (top to bottom), bottom row (right to left), left column (bottom to top).
- Step 9: Rotate: rotatedBoundary[(i + K) % L] = boundary[i] for every i.
- Step 10: Copy the original matrix and write rotatedBoundary back into the boundary cells in the same clockwise order.
- Step 11: Display the rotated matrix.
- Step 12: Take the four corners of the rotated matrix and count the primes using the isPrime() method.
- Step 13: Display the prime corner count. Stop.
Variable description
| Name | Data type | Purpose |
|---|---|---|
m | int | number of rows |
n | int | number of columns |
mat | int[][] | the original matrix |
k | int | number of positions to rotate the boundary clockwise |
boundaryLength | int | number of boundary cells, L = 2(m + n) - 4 |
boundary | int[] | boundary elements in clockwise order |
rotatedBoundary | int[] | boundary elements after the rotation |
rotatedMat | int[][] | the rotated matrix (inner cells copied unchanged) |
corners | int[] | the four corners of rotatedMat |
primeCount | int | number of corners that are prime |
idx | int | position in boundary while copying and writing back |
i, j | int | loop counters |
Methods in the program
- isPrime(int num): returns true for a prime number. Returns false for 0, 1 and any number with a factor.
- displayMatrix(int[][] mat, int m, int n): prints a matrix with m rows and n columns.
Java solution
import java.util.Scanner;
/**
* ISC Computer Science Practical Examination
* Topic: 2D Arrays (Matrix Boundary Rotation)
* Class: MatrixBoundaryRotation
*/
public class MatrixBoundaryRotation {
// Helper method to check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) return false;
if (num == 2 || num == 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true;
}
// Helper method to display a matrix
public static void displayMatrix(int[][] mat, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%4d", mat[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Input dimensions
System.out.print("M = ");
int m = in.nextInt();
System.out.print("N = ");
int n = in.nextInt();
// 1. Validate matrix dimensions (2 < M < 10 and 2 < N < 10)
if (m <= 2 || m >= 10 || n <= 2 || n >= 10) {
System.out.println("INVALID SIZE");
return;
}
// Input matrix elements
int[][] mat = new int[m][n];
System.out.println("ENTER ELEMENTS:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
mat[i][j] = in.nextInt();
if (mat[i][j] <= 0) {
System.out.println("INVALID ELEMENT: Must be positive integer");
return;
}
}
}
// Input rotation parameter K
System.out.print("K = ");
int k = in.nextInt();
int boundaryLength = 2 * (m + n) - 4;
// 2. Validate K (1 <= K < 2(M + N) - 4)
if (k < 1 || k >= boundaryLength) {
System.out.println("INVALID VALUE OF K");
return;
}
// Display Original Matrix
System.out.println("\nORIGINAL MATRIX");
displayMatrix(mat, m, n);
// 3. Extract Boundary into 1D Array (Clockwise Order)
int[] boundary = new int[boundaryLength];
int idx = 0;
// Top Row (Left to Right): [0][0] to [0][N-1]
for (int j = 0; j < n; j++) {
boundary[idx++] = mat[0][j];
}
// Right Column (Top to Bottom): [1][N-1] to [M-1][N-1]
for (int i = 1; i < m; i++) {
boundary[idx++] = mat[i][n - 1];
}
// Bottom Row (Right to Left): [M-1][N-2] down to [M-1][0]
for (int j = n - 2; j >= 0; j--) {
boundary[idx++] = mat[m - 1][j];
}
// Left Column (Bottom to Top): [M-2][0] down to [1][0]
for (int i = m - 2; i >= 1; i--) {
boundary[idx++] = mat[i][0];
}
// 4. Rotate 1D Array Clockwise by K positions
// Clockwise shift: an element at index i moves to (i + K) % boundaryLength
int[] rotatedBoundary = new int[boundaryLength];
for (int i = 0; i < boundaryLength; i++) {
rotatedBoundary[(i + k) % boundaryLength] = boundary[i];
}
// 5. Create Rotated Matrix (Copy original inner elements, replace boundary)
int[][] rotatedMat = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
rotatedMat[i][j] = mat[i][j];
}
}
// Re-inject rotated boundary in the same clockwise order
idx = 0;
// Top Row
for (int j = 0; j < n; j++) {
rotatedMat[0][j] = rotatedBoundary[idx++];
}
// Right Column
for (int i = 1; i < m; i++) {
rotatedMat[i][n - 1] = rotatedBoundary[idx++];
}
// Bottom Row
for (int j = n - 2; j >= 0; j--) {
rotatedMat[m - 1][j] = rotatedBoundary[idx++];
}
// Left Column
for (int i = m - 2; i >= 1; i--) {
rotatedMat[i][0] = rotatedBoundary[idx++];
}
// Display Rotated Matrix
System.out.println("\nMATRIX AFTER ROTATING THE BOUNDARY BY " + k);
displayMatrix(rotatedMat, m, n);
// 6. Check Corner Elements for Primes
int[] corners = {
rotatedMat[0][0],
rotatedMat[0][n - 1],
rotatedMat[m - 1][n - 1],
rotatedMat[m - 1][0]
};
int primeCount = 0;
for (int c : corners) {
if (isPrime(c)) {
primeCount++;
}
}
System.out.println("\nNUMBER OF PRIME CORNERS: " + primeCount);
}
}
Sample runs
Each run uses the program above. The output is copied from the terminal.
Worked example: 3 x 4 matrix, K = 2
Input, typed in this order
3 4 1 2 3 4 5 6 7 8 9 10 11 12 2
Output
M = N = ENTER ELEMENTS: K = ORIGINAL MATRIX 1 2 3 4 5 6 7 8 9 10 11 12 MATRIX AFTER ROTATING THE BOUNDARY BY 2 9 5 1 2 10 6 7 3 11 12 8 4 NUMBER OF PRIME CORNERS: 2
Invalid size: M = 2
Input, typed in this order
2 5
Output
M = N = INVALID SIZE
Invalid K: 3 x 3 matrix, K = 8 (boundary length is 8)
Input, typed in this order
3 3 1 2 3 4 5 6 7 8 9 8
Output
M = N = ENTER ELEMENTS: K = INVALID VALUE OF K