2080 11

CSC419 · 2080 · 5 marks

Suppose that 9 integers are written in a file named 'magic.txt' in the arrangement of 3 x 3 separated by space. Write a program to check whether the integers in all rows, all columns, and both diagonals sum to the same constant or not.

Suppose that 9 integers are written in a file named 'magic.txt' in the arrangement of 3 x 3 separated by space. Write a program to check whether the integers in all rows, all columns, and both diagonals sum to the same constant or not. [5]

Advanced Java ProgrammingChapter 1: Programming in JavaWorking with FilesNumerical: magic square program

Given

  • Input file: magic.txt
  • Contains 9 integers arranged as a $3 \times 3$ grid, separated by spaces.
  • Task: Verify whether all 3 rows, all 3 columns, and both diagonals sum to the same constant (magic square check).

No specific numeric values are given for the integers in the question; the file contents are supplied at runtime. This is a programming problem, so no numeric computation is required unless sample data is chosen for demonstration.


STEP 2 - SOLVE: Model Answer

Approach

  1. Read the 9 integers from magic.txt into a $3 \times 3$ array.
  2. Compute the target constant using the sum of the first row.
  3. Compare every row sum, every column sum, and both diagonal sums against this constant.
  4. Report whether it is a magic square.

Java Program

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MagicSquareChecker {
    public static void main(String[] args) {
        int[][] m = new int[3][3];

        // Step 1: Read 9 integers from magic.txt
        try (Scanner sc = new Scanner(new File("magic.txt"))) {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    if (sc.hasNextInt())
                        m[i][j] = sc.nextInt();
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
            return;
        }

        // Display the matrix
        System.out.println("Matrix read from magic.txt:");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++)
                System.out.print(m[i][j] + "\t");
            System.out.println();
        }

        // Step 2: Target constant = sum of first row
        int constant = m[0][0] + m[0][1] + m[0][2];
        boolean isMagic = true;

        // Step 3: Check all rows
        for (int i = 0; i < 3; i++) {
            int rowSum = m[i][0] + m[i][1] + m[i][2];
            if (rowSum != constant) isMagic = false;
        }

        // Check all columns
        for (int j = 0; j < 3; j++) {
            int colSum = m[0][j] + m[1][j] + m[2][j];
            if (colSum != constant) isMagic = false;
        }

        // Check main diagonal
        int diag1 = m[0][0] + m[1][1] + m[2][2];
        if (diag1 != constant) isMagic = false;

        // Check anti-diagonal
        int diag2 = m[0][2] + m[1][1] + m[2][0];
        if (diag2 != constant) isMagic = false;

        // Step 4: Final result
        if (isMagic)
            System.out.println("All rows, columns and diagonals sum to "
                               + constant + ". It IS a Magic Square.");
        else
            System.out.println("The sums are NOT all equal. It is NOT a Magic Square.");
    }
}

Sample magic.txt

2 7 6
9 5 1
4 3 8

Verification of Sums (for the sample)

Rows:

  • $2+7+6 = 15$
  • $9+5+1 = 15$
  • $4+3+8 = 15$

Columns:

  • $2+9+4 = 15$
  • $7+5+3 = 15$
  • $6+1+8 = 15$

Diagonals:

  • Main: $2+5+8 = 15$
  • Anti: $6+5+4 = 15$

All eight sums equal $15$, the magic constant.

Sample Output

Matrix read from magic.txt:
2    7    6
9    5    1
4    3    8
All rows, columns and diagonals sum to 15. It IS a Magic Square.

Key Points

CheckFormula
Row $i$$m[i][0]+m[i][1]+m[i][2]$
Column $j$$m[0][j]+m[1][j]+m[2][j]$
Main diagonal$m[0][0]+m[1][1]+m[2][2]$
Anti-diagonal$m[0][2]+m[1][1]+m[2][0]$

The program is logically correct: it reads from the file, catches FileNotFoundException, and compares all eight sums to the first-row constant. The sample result gives a magic constant of $15$.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.