ISC practical papers › ISC 2014 › Question 2
Symmetry in a 2D Matrix
ISC 2014 Computer Science Practical, Question 2 · Arrays and matrices
The question
Write a program to declare a square matrix A[][] of order MxM 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 an invalid input. Allow the user to input integers into this matrix. Perform the following tasks: a) Display the original matrix. b) Check if the given matrix is symmetric or not. A square matrix is said to be symmetric, if the element in the i th row and j th column is equal to the element of the j th row and i th column. c) Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of the matrix and display them. Test your program for the following data and some random data:
Example 1
INPUT: M = 3
1 2 3
2 4 5
3 5 6
OUTPUT: ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10
Example 2
INPUT: M = 4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT: ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal = 17
The sum of the right diagonal = 20
Example 3 INPUT: M = 12 OUTPUT: THE MATRIX SIZE IS OUT OF RANGE
Question: ISC 2014 Computer Science Paper 2 (Practical), Question 2, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class Matrix {
private int A[][], M;
public Matrix(int M) {
this.M = M;
A = new int[M][M];
}
public String toString() {
String output = "";
for (int row = 0; row < M; row++) {
output += "\t";
for (int col = 0; col < M; col++) {
output += A[row][col];
if (col < M - 1)
output += "\t";
}
output += "\n";
}
return output;
}
public void getInput(){
Scanner in = new Scanner( System.in);
for(int row=0; row<M; row++){
for( int col=0; col<M; col++){
A[row][col]=in.nextInt();
}
}
in.close();
}
public boolean isSymmetric(){
for(int row=0; row<M; row++){
for( int col=0; col<row; col++){
if( A[row][col]!=A[col][row]) return false;
}
}
return true;
}
public void computeAndDisplaySumOfDiagonals(){
int sumOfLeftDiagonal=0, sumOfRightDiagonal=0;
for( int i=0; i<M;i++){
sumOfLeftDiagonal+=A[i][i];
sumOfRightDiagonal+=A[i][M-1-i];
}
System.out.println("The sum of the left diagonal = "+sumOfLeftDiagonal);
System.out.println("The sum of the right diagonal = "+sumOfRightDiagonal);
}
}
class ISC2014Q2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tM = ");
int M = in.nextInt();
if (M > 2 && M < 10) {
Matrix objMatrix = new Matrix(M);
objMatrix.getInput();
System.out.println("OUTPUT:\tORIGINAL MATRIX");
System.out.println(objMatrix);
if(objMatrix.isSymmetric()){
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
}else{
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
}
objMatrix.computeAndDisplaySumOfDiagonals();
} else {
System.out.println("OUTPUT:\tTHE MATRIX SIZE IS OUT OF RANGE");
}
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.