Introduction
The ISC Computer Science Practical 2015 Question 2 focuses on two-dimensional arrays and matrix manipulation in Java. It is an important programming exercise for students preparing for ISC Computer Science practical examinations.
In this problem, students are required to accept a square matrix from the user, rotate it 90 degrees clockwise and calculate the sum of its four corner elements.
The program also involves input validation to ensure that the matrix size satisfies the given conditions.
In this tutorial, we will understand the problem statement, develop the algorithm, write the complete Java program, and examine its working through examples and a detailed explanation.
Problem Statement – ISC Computer Science Practical 2015 Question 2
Write a program to declare a square matrix A[][] of order M × M, where M is the number of rows and the number of columns, such that M must be greater than 2 and less than 10.
Accept the value of M as user input. Display an appropriate message for invalid input.
Allow the user to input integers into the matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Rotate the matrix 90° clockwise.
(c) Find the sum of the elements of the four corners of the matrix.
Test the program with the sample data and some random data.
Input Restrictions
- M must be greater than 2.
- M must be less than 10.
- Therefore, the acceptable matrix order ranges from 3 to 9.
- All matrix elements must be integers.
If the matrix size does not satisfy these conditions, display an appropriate error message.
- Understanding the Matrix Rotation Concept
Before writing the Java program, let us understand how a square matrix is rotated 90 degrees clockwise.
Consider the following 3 × 3 matrix:
Original Matrix
3 | 4 | 9 |
2 | 5 | 8 |
1 | 6 | 7 |
After rotating the matrix 90 degrees clockwise, the resulting matrix becomes:
Matrix After 90° Clockwise Rotation
1 | 2 | 3 |
6 | 5 | 4 |
7 | 8 | 9 |
Notice that the last element of each original column moves to the beginning of the corresponding row in the rotated matrix.
Formula for 90° Clockwise Matrix Rotation
For an original matrix a[][] and a rotated matrix b[][], the position of each element changes according to the following formula:
b[j][M – 1 – i] = a[i][j];
Here:
- i represents the row index of the original matrix.
- j represents the column index of the original matrix.
- M represents the order of the square matrix.
This formula transfers every element from its original position to its correct position in the rotated matrix.
Algorithm
The following algorithm describes the logical steps required to solve the problem.
- Start.
- Declare two two-dimensional integer arrays, a[][] and b[][].
- Accept the order M of the square matrix.
- Check whether M is greater than 2 and less than 10.
- If the condition is not satisfied, display SIZE OUT OF RANGE.
- Otherwise, perform the following operations:
- Create two square matrices of order M × M.
- Accept integer elements into the original matrix a[][].
- Display the original matrix.
- Rotate the original matrix 90 degrees clockwise using the formula b[j][M-1-i] = a[i][j].
- Display the rotated matrix.
- Calculate the sum of the four corner elements.
- Display the sum of the corner elements.
- Stop.
- Java Program – Question2_2015
The following Java program uses two-dimensional arrays and nested for loops to accept, display and rotate the matrix.
A separate matrix is used to store the rotated elements, making the rotation process easier to understand and implement.
- Explanation of the Java Program
Let us understand the important sections of the program.
- Declaring Variables and Accepting Matrix Size
int M, i, j, sum = 0;
The variables are used as follows:
Variable | Purpose |
M | Stores the order of the square matrix |
i | Controls the row index |
j | Controls the column index |
sum | Stores the sum of four corner elements |
The matrix order is accepted using:
M = sc.nextInt();
- Validating the Matrix Size
if(M <= 2 || M >= 10)
{
System.out.println(“SIZE OUT OF RANGE”);
}
This condition ensures that the matrix order is within the permitted range.
For example:
- M = 2: Invalid
- M = 3: Valid
- M = 5: Valid
- M = 9: Valid
- M = 10: Invalid
The logical OR operator (||) checks whether either of the two invalid conditions is true.
- Declaring Two-Dimensional Arrays
int a[][] = new int[M][M];
int b[][] = new int[M][M];
Two square matrices are created:
- a[][] stores the original matrix.
- b[][] stores the matrix after rotation.
Both arrays have M rows and M columns.
- Accepting Matrix Elements
for(i = 0; i < M; i++)
{
for(j = 0; j < M; j++)
{
a[i][j] = sc.nextInt();
}
}
Nested for loops are used to accept the matrix elements.
The outer loop controls the rows, while the inner loop controls the columns.
- Displaying the Original Matrix
for(i = 0; i < M; i++)
{
for(j = 0; j < M; j++)
{
System.out.print(a[i][j] + “\t”);
}
System.out.println();
}
Each row is displayed using the inner loop. The println() statement moves the cursor to the next line after displaying a complete row.
- Rotating the Matrix 90 Degrees Clockwise
This is the most important part of the program.
for(i = 0; i < M; i++)
{
for(j = 0; j < M; j++)
{
b[j][M – 1 – i] = a[i][j];
}
}
The nested loops visit every element of the original matrix.
The rotation formula:
b[j][M – 1 – i] = a[i][j];
changes the position of each element to produce a clockwise rotation.
For example, when M = 3:
Original Position | New Position | Element |
a[0][0] | b[0][2] | 3 |
a[0][1] | b[1][2] | 4 |
a[0][2] | b[2][2] | 9 |
a[1][0] | b[0][1] | 2 |
a[1][1] | b[1][1] | 5 |
a[1][2] | b[2][1] | 8 |
a[2][0] | b[0][0] | 1 |
a[2][1] | b[1][0] | 6 |
a[2][2] | b[2][0] | 7 |
After all elements are repositioned, the rotated matrix becomes:
1 2 3
6 5 4
7 8 9
- Calculating the Sum of Four Corner Elements
sum = b[0][0] + b[0][M-1] +
b[M-1][0] + b[M-1][M-1];
The four corner positions of a square matrix are:
Position | Array Index |
Top-left corner | b[0][0] |
Top-right corner | b[0][M-1] |
Bottom-left corner | b[M-1][0] |
Bottom-right corner | b[M-1][M-1] |
The values stored at these positions are added and the result is displayed.
Note: The four corners of a matrix remain corners after a 90-degree rotation. Therefore, their sum is the same whether calculated from the original matrix or the rotated matrix.
- Dry Run of ISC Computer Science Practical 2015 Question 2
Let us trace the program using the first example given in the question.
Input
M = 3
3 4 9
2 5 8
1 6 7
Step 1: Original Matrix
The program accepts the nine elements and stores them in a[][].
3 4 9
2 5 8
1 6 7
Step 2: Matrix Rotation
The rotation formula is applied:
b[j][M – 1 – i] = a[i][j];
The resulting matrix is:
1 2 3
6 5 4
7 8 9
Step 3: Finding the Sum of Corner Elements
The four corner elements are:
- Top-left = 1
- Top-right = 3
- Bottom-left = 7
- Bottom-right = 9
Calculation:
Sum = 1 + 3 + 7 + 9
Sum = 20
- Sample Input and Output
Example 1 – Matrix of Order 3
Input:
Enter the order of matrix: 3
Enter the elements of matrix:
3 4 9
2 5 8
1 6 7
Output:
ORIGINAL MATRIX
3 4 9
2 5 8
1 6 7
MATRIX AFTER ROTATION
1 2 3
6 5 4
7 8 9
Sum of the corner elements = 20
Example 2 – Matrix of Order 4
Input:
Enter the order of matrix: 4
Enter the elements of matrix:
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5
Output:
ORIGINAL MATRIX
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5
MATRIX AFTER ROTATION
3 1 2 1
7 6 5 2
6 7 8 4
5 4 3 9
Sum of the corner elements = 18
The corner elements are 3, 1, 5 and 9.
Therefore:
Sum = 3 + 1 + 5 + 9
Sum = 18
Example 3 – Invalid Matrix Size
Input:
Enter the order of matrix: 14
Output:
SIZE OUT OF RANGE
The program displays this message because the matrix order must be greater than 2 and less than 10.
- Important Java Concepts Used
The following concepts are particularly important for understanding and solving this programming problem.
Java Concept | Application |
Scanner class | Accepting input from the user |
Two-dimensional arrays | Storing matrix elements |
Nested for loops | Accepting and displaying matrix elements |
if-else statement | Validating the matrix order |
Array indexing | Accessing individual matrix elements |
Modifying array positions | Rotating the matrix |
Arithmetic operators | Calculating the corner sum |
Important Matrix Rotation Formulas
For students preparing for ISC Computer Science practical examinations, the following formulas are useful:
90° Clockwise Rotation
b[j][M-1-i] = a[i][j];
90° Anticlockwise Rotation
b[M-1-j][i] = a[i][j];
Understanding the difference between these two formulas makes matrix rotation problems easier to solve.
Conclusion
The ISC Computer Science Practical 2015 Question 2 is an excellent exercise for understanding two-dimensional arrays and matrix manipulation in Java.
The problem combines input validation, nested loops, array indexing, matrix rotation and arithmetic operations.
The most important part is understanding the clockwise rotation formula:
b[j][M-1-i] = a[i][j];
Once students understand how the row and column indices change during rotation, implementing the program becomes straightforward.
Practising this problem will also help students develop a stronger understanding of two-dimensional arrays, which are frequently used in computer science practical examinations.