Question

In: Computer Science

THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers...

THIS IS JAVA

Magic squares.

An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.

Write a program that randomly generates 16 numbers, and it assigns them to the array after testing that the number was not already assigned. The program should test whether they form a magic square when put into a 4 × 4 array.

You need to test two features:

1. Build the array so that each of the numbers 1, 2, ..., 16 is present?

2.When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other? If so, display an appropriate message.

Solutions

Expert Solution

package test;

import java.util.ArrayList;
import java.util.Random;

public class magicMatrix {
  
  
   // method to check if the matrix is magic
   public static boolean magicCheck(int[][] mat) {
       int sum = rowSum(mat,0);
       for(int i=0;i<4;i++) {
           // check if mismatch in sum occurs in rows
           if(sum != rowSum(mat,i)) {
               return false;
           }
       }
       for(int i=0;i<4;i++) {
           // check if mismatch in sum occurs in cols
           if(sum != colSum(mat,i)) {
               return false;
           }
       }
       // check if mismatch in sum occurs in diagonal in left direction
       if(sum!= diagLSum(mat))
           return false;
       // check if mismatch in sum occurs in diagonal in right direction
       if(sum!=diagRSum(mat))
           return false;
       return true;
      
   }
   //calculates row sum
   public static int rowSum(int[][] mat,int r) {
       int sum=0;
       for(int i=0;i<4;i++) {
           sum+=mat[r][i];
       }
       return sum;
   }
   //calculates column sum
   public static int colSum(int[][] mat,int c) {
       int sum=0;
       for(int i=0;i<4;i++) {
           sum+=mat[i][c];
       }
       return sum;
   }
   public static int diagLSum(int[][] mat) {
       int sum=0;
       for(int i=0;i<4;i++) {
           sum+=mat[i][i];
       }
       return sum;
      
   }
   //calculates diagonal sum in right diagonal
   public static int diagRSum(int[][] mat) {
       int sum=0;
       for(int i=0;i<4;i++) {
           sum+= mat[i][3-i];
       }
       return sum;
   }
  
   //prints the matrix
   public static void printMat(int[][] mat) {
       for(int i=0;i<4;i++) {
           for(int j=0;j<4;j++) {
               System.out.print(" "+mat[i][j]);
           }
           System.out.println("\n");
       }
   }
   static Random rand;

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       rand = new Random();
       rand.setSeed(System.nanoTime());
       //creates an array of integers
       ArrayList<Integer> array = new ArrayList<Integer>();
       //adds the randomly generated numbers from 1 to 16 in the array
       for(int i=0;i<16;) {
           int y=rand.nextInt(16)+1;
           if(!array.contains(y)) {
               array.add(y);
               i++; // increments i if y is added to the array
           }
       }
       //declares the matrix
       int[][] matrix= new int[4][4];
      
       //adds integers from array into matrix
       for(int i=0;i<4;i++) {
           for(int j=0;j<4;j++) {
               matrix[i][j]=array.get(i*4+j);
           }
          
       }
       //prints the matrix
       System.out.println("The matrix generated is -> ");
       printMat(matrix);
      
       //checks if the matrix is magic or not and displays the appropriate message
       if(magicCheck(matrix)) {
           System.out.println("This matrix is a magic matrix");
       }else {
           System.out.println("This matrix is not a magic matrix");
       }

   }

}

==========================================================

above is result of one run..

Thanks

The matrix generated is -> 1 15 8 6 12 9 2 5 13 11 10 4 7 14 16 3 This matrix is not a magic matrix


Related Solutions

A special chessboard is 2 squares wide and n squares long. Using n dominoes that are...
A special chessboard is 2 squares wide and n squares long. Using n dominoes that are 1 square by 2 squares, there are many ways to completely cover this chessboard with no overlap. How many are there? Prove your answer.
Show that there are no magic squares of order greater than one if we require each...
Show that there are no magic squares of order greater than one if we require each row, column and diagonal to have a constant product.
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the...
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the user places the ball face down, asks a yes-or-no question and turns the ball face up to reveal the answer. The standard magic 8 ball has 20 standard answers shown below, where 10 are positive (green), 5 are non-committal (yellow), and 5 are negative (red) (credit: Wikipedia) Write a program that does the following: 1. Stores all the responses in a String array using...
Suppose C is a m × n matrix and A is a n × m matrix....
Suppose C is a m × n matrix and A is a n × m matrix. Assume CA = Im (Im is the m × m identity matrix). Consider the n × m system Ax = b. 1. Show that if this system is consistent then the solution is unique. 2. If C = [0 ?5 1 3 0 ?1] and A = [2 ?3   1 ?2    6 10] ,, find x (if it exists) when (a) b =[1...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
Matrix A belongs to an n×n matrix over F. show that there exists a nonzero polynomial...
Matrix A belongs to an n×n matrix over F. show that there exists a nonzero polynomial f(x) belongs to F[x] such that f(A) =0.
A magic number is the name given to certain numbers of protons or neutrons that have...
A magic number is the name given to certain numbers of protons or neutrons that have a high correlation to stability in nuclei: magic numbers of protons:magic numbers of neutrons:2,8,20,28,50,822,8,20,28,50,82,126,184 A plot of the number of neutrons versus the number of protons for all known stable nuclei, represented by reddish brown dots on the graph, shows that these nuclei cluster together in a region known as the valley of stability. Nuclei with a ratio that is too high tend to...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate the sum of each column. Use subscript notation to find the 6th element and the last element of each magic square. Find the maximum of all the elements in each magic square.
Let A be an m × n matrix and B be an m × p matrix....
Let A be an m × n matrix and B be an m × p matrix. Let C =[A | B] be an m×(n + p) matrix. (a) Show that R(C) = R(A) + R(B), where R(·) denotes the range of a matrix. (b) Show that rank(C) = rank(A) + rank(B)−dim(R(A)∩R(B)).
Prove: An (n × n) matrix A is not invertible ⇐⇒ one of the eigenvalues of...
Prove: An (n × n) matrix A is not invertible ⇐⇒ one of the eigenvalues of A is λ = 0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT