Question

8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic square if the sum of the elements in each row, in

Write a method public static Boolean sameSet(int[] a, int[] b) that checks whether two arrays have the same elements in some
0 0
Add a comment Improve this question Transcribed image text
Answer #1

8. Given Below is the java program for the checking 4 by 4 matrix is a magic square or not :

---------------------------------------------------------------------------------------------------------------

import java.util.*;
import java.lang.*;
import java.io.*;

public class test {
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);     //creating input reader
        PrintWriter w = new PrintWriter(System.out);     //creating writer

        int[][] mat = new int[4][4];                     //declare the 4 by 4 input matrix

        for (int i = 0; i < 4; i++) {                   //taking input for matrix
            for (int j = 0; j < 4; j++) {
                mat[i][j] = in.nextInt();
            }
        }
        HashSet<Integer> sumValues = new HashSet<>(); //create a hashset to store all calculated sum

        for (int i = 0; i < 4; i++) {                   //taking input for matrix

            int rowSum = 0, columnSum = 0;              // variables to calculate row and column sum repectively
            for (int j = 0; j < 4; j++) {
                rowSum += mat[i][j];                    // calculating row sum
                columnSum += mat[j][i];                 //calculating column sum
            }

            // adding row and column values to hashset
            sumValues.add(rowSum);
            sumValues.add(columnSum);
        }

        int leftDiagonalsum = mat[0][0] + mat[1][1] + mat[2][2] + mat[3][3];    //calculating left diagonam sum
        int rightDiagonalsum = mat[0][3] + mat[1][2] + mat[2][1] + mat[3][0];   //calculating right diagonam sum

        //adding diagonal sum to hashset
        sumValues.add(leftDiagonalsum);
        sumValues.add(rightDiagonalsum);

        //if all sum values are same the hashset should contain only single value
        if (sumValues.size() == 1) {
            w.println("Yes, Its a magic square");
        }

        //otherwise atleast 2 sumvalues are different
        else {
            w.println("No, Its not a magic square");
        }

        w.close();
    }

-----------------------------------------------------------------------------------------------------------------

=> Find the output and compilation/running screenshot below

java>javac test.java :java>java test 16 3 2 13 5 10 11 8 96 7 12 4 15 14 1 es, Its a magic square C: java>

7. The the required method below :

---------------------------------------------------------------------------------------------------------------

    public static Boolean sameSet(int[] a, int[] b) {
        HashSet<Integer> hsa = new HashSet<>(), hsb = new HashSet<>(); //creating HashSet for both arrays
        for (int x : a)                                                //filling hashSet for array a
            hsa.add(x);

        for (int x : b)                                                //filling hashSet for array b
            hsb.add(x);


        return hsa.equals(hsb);                                        // check if 2 hashsets are equal
    }

---------------------------------------------------------------------------------------------------------------

For running and compiling of the program ,find the screenshot below :

Command Prompt :java>javac test.java :java> java test 1 2 345 6 1 2 345 1 true java>java test 1 234 5 1 234 6 false :java>

Add a comment
Know the answer?
Add Answer to:
8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is...

    C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 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. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square, place k+1 into the square...

  • Java Magic Square: A n x n matrix that is filled with the numbers 1, 2,...

    Java Magic Square: A n x 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. I need to write an application that gets 16 values as user input, in any order. Store these values in an ArrayList. When the numbers are put into a square, this would be a 4x4 two-dimensional array....

  • Magic Squares; Java code

    Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum ofthe elements in each row, in each column, and in the two diagonals is the same value. For example,16 3 2 135 10 11 89 6 7 124 15 14 1Write a program that reads in n2 values from the keyboard and tests whether they form a magic squarewhen arranged as a square matrix. You...

  • I need this written in C++. Magic Squares. An n x n matrix that is filled...

    I need this written in C++. Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 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. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square,...

  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagona...

    how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagonals have the same sum. For example, A- 3 5 7 4 9 2 Complete the following magic matrices 17? ?? 3 ? 2 ? 2? ? Do the following steps in each case: 1. Write the system of equations and put it under the...

  • Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement...

    Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement of the numbers 1, 2, ., N2, in which the sum of rows, columns, and diagonals are the same. The users (i.e., TAs) will specify the size of the square matrix: N. The value N must be an odd number between 3 and 15. Example Run For example, you program is expected to run as the following way. NOTE: We only list 5 magic...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • Magic Square question for Python

    You have this solution in Java, I am interested in this same solution for Python.One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.You should find that the first, second, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT