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
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 :
8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic...
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, 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. 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 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 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...
PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...
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 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 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 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...