ISC practical papers › ISC 2005 › Question 2
Test For Wondrous Square (Magic Square)
ISC 2005 Computer Science Practical, Question 2 · Arrays and matrices
The question
A wondrous square is an n by n grid which fulfills the following conditions: (i) It contains integers from 1 to n^2, where each integer appears only once. (ii) The sum of integers in any row or column must add up to 0.5 * n * (n^2 + 1).
For example the following grid is a wondrous square where the sum of each row or column is 65 when n = 5:
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Write a program to read n (2 < n < 10) and the values stored in these n by n cells and output if the grid represents a wondrous square or not.
Also output all the prime numbers in the grid along with their row index and column index as shown in the output.
A natural number is said to be prime if it has exactly two divisors. E.g. 2,3,5,7,11 ………..
The first element of the given grid i.e 17 is stored at row index 0 and column index 0 and the next element in the row i.e. 24 is stored at row index 0 and column index 1.
Test your program for the following data and some random data.
SAMPLE DATA:
INPUT: N = 4 16 15 1 2 6 4 10 14 9 8 12 5 3 7 11 13 OUTPUT: YES IT REPRESENTS A WONDROUS SQUARE. PRIME ROW INDEX COLUMN INDEX 2 0 3 3 3 0 5 2 3 7 3 1 11 3 2 13 3 3 15 0 1 <-- 15 is not a prime
INPUT: N = 3 1 2 4 3 7 5 8 9 6 OUTPUT: NOT A WONDROUS SQUARE. PRIME ROW INDEX COLUMN INDEX 2 0 1 3 1 0 5 1 2 7 1 1
INPUT: N = 2 2 3 3 2 OUTPUT: NOT A WONDROUS SQUARE. PRIME ROW INDEX COLUMN INDEX 2 0 0 3 0 1 3 1 0 2 1 1
Question: ISC 2005 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 Square {
private int a[][], N, sum;
public Square(int a[][]) {
this.a = a;
this.N = a.length;
sum = (int)(0.5 * N * (N*N+1));
}
public boolean isWondrousSquare(){
boolean flag[] = new boolean[N*N];
int sumOfRows, sumOfCols;
for (int row = 0; row < N; row++) {
sumOfRows= sumOfCols=0;
for (int col = 0; col < N; col++) {
sumOfRows += a[row][col];
sumOfCols += a[col][row];
if( flag[a[row][col] -1 ] ) return false;
flag[a[row][col] -1 ] = true;
}
if( sumOfRows!=sum || sumOfCols!=sum) return false;
}
return true;
}
public String toString(){
String output="PRIME\tROW INDEX\tCOLUMN INDEX\n";
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if(isPrime(a[row][col])){
output+=a[row][col]+"\t"+row+"\t\t"+col+"\n";
}
}
}
return output;
}
private static boolean isPrime(int n){
if(n<2) return false;
else if(n==2) return true;
else if(n%2==0) return false;
else{
int limit=(int)Math.sqrt(n);
for( int i = 3; i<=limit; i+=2){
if(n%i==0) return false;
}
}
return true;
}
}
public class ISC2005Q2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\nN = ");
int N = in.nextInt();
int a[][] = new int[N][N];
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
a[row][col] = in.nextInt();
}
}
Square sq = new Square(a);
System.out.println("OUPUT:");
if (sq.isWondrousSquare()) {
System.out.println("YES IT REPRESENTS A WONDROUS SQUARE.");
} else {
System.out.println("NOT A WONDROUS SQUARE.");
}
System.out.println(sq);
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.