e 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 both diagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. Write a program to determine whether a series of square matrices are magic or not. The first line of input for each square specifies the size of the square(number of rows and columns). The square elements follow, one row per line. The end of the data is indicated by -1. Create a class called Square that has methods to construct a square of a specified size, to read in the elements of t

Answers

Answer 1
Answer:

Answer:

Check the explanation

Explanation:

// Define a Square class with methods to create and read in

// info for a square matrix and to compute the sum of a row,

// a column, either diagonal, and whether it is magic.

//

// ****************************************************************

import java.util.Scanner;

import java.io.*;

public class Square {

int[][] square;

//--------------------------------------

//create new square of given size

//--------------------------------------

public Square(int size) {

square = new int[size][size];

}

//-----------------------------------------------

//return the sum of the values in the given row

//-----------------------------------------------

public int sumRow(int row) {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[row][i];

}

return sum;

}

//-------------------------------------------------

//return the sum of the values in the given column

//-------------------------------------------------

public int sumCol(int col) {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[i][col];

}

return sum;

}

//---------------------------------------------------

//return the sum of the values in the main diagonal

//---------------------------------------------------

public int sumMainDiag() {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[i][i];

}

return sum;

}

//---------------------------------------------------------------

//return the sum of the values in the other ("reverse") diagonal

//---------------------------------------------------------------

public int sumOtherDiag() {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[square.length - i - 1][i];

}

return sum;

}

//-------------------------------------------------------------------

//return true if the square is magic (all rows, cols, and diags have

//same sum), false otherwise

//-------------------------------------------------------------------

public boolean magic() {

// Add your code here. Check if the sum of main diagonal equals the other diagonal,

// also if all rows and all columns sums equal to the diagonal as well. Any uneuqal will

// terminate the comparison.

int d1 = sumMainDiag();

int d2 = sumOtherDiag();

if (d1 != d2) {

return false;

}

for (int i = 0; i < square.length; i++) {

if (d1 != sumRow(i) || d1 != sumCol(i)) {

return false;

}

}

return true;

}

//----------------------------------------------------

//read info into the square from the standard input.

//----------------------------------------------------

public void readSquare(Scanner scan) {

for (int row = 0; row < square.length; row++) {

for (int col = 0; col < square.length; col++) {

square[row][col] = scan.nextInt();

}

}

}

//---------------------------------------------------

//print the contents of the square, neatly formatted

//---------------------------------------------------

public void printSquare() {

for (int row = 0; row < square.length; row++) {

for (int col = 0; col < square.length; col++) {

System.out.print(square[row][col] + "\t");

}

System.out.println();

}

}

}

// ****************************************************************

// SquareTest.java

//

// Uses the Square class to read in square data and tell if

// each square is magic.

//

// ****************************************************************

class SquareTest {

public static void main(String[] args) throws IOException {

File file = new File("magicData.txt");

Scanner scan = new Scanner(file);

int count = 1; //count which square we're on

int size = scan.nextInt(); //size of next square

//Expecting -1 at bottom of input file

while (size != -1) {

//create a new Square of the given size

Square magicSquare = new Square(size);

//call its read method to read the values of the square

magicSquare.readSquare(scan);

System.out.println("\n******** Square " + count + " ********");

//print the square

magicSquare.printSquare();

//print the sums of its rows

for (int row = 0; row < size; row++) {

System.out.println("Sum of row " + row + ": "

+ magicSquare.sumRow(row));

}

//print the sums of its columns

for (int col = 0; col < size; col++) {

System.out.println("Sum of column " + col + ": "

+ magicSquare.sumCol(col));

}

//print the sum of the main diagonal

System.out.println("Sum of the main diagonal: "

+ magicSquare.sumMainDiag());

//print the sum of the other diagonal

System.out.println("Sum of the other diagonal: "

+ magicSquare.sumOtherDiag());

//determine and print whether it is a magic square

if (magicSquare.magic()) {

System.out.println("It's a magic square!");

} else {

System.out.println("It's not a magic square!");

}

System.out.println();

//get size of next square

size = scan.nextInt();

count++;

}

}

}

Answer 2
Answer:

Answer:

See explaination

Explanation:

/ Define a Square class with methods to create and read in

// info for a square matrix and to compute the sum of a row,

// a column, either diagonal, and whether it is magic.

//

// ************************************************************

import java.util.Scanner;

import java.io.*;

public class Square {

int[][] square;

//--------------------------------------

//create new square of given size

//--------------------------------------

public Square(int size) {

square = new int[size][size];

}

//-----------------------------------------------

//return the sum of the values in the given row

//-----------------------------------------------

public int sumRow(int row) {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[row][i];

}

return sum;

}

//-------------------------------------------------

//return the sum of the values in the given column

//-------------------------------------------------

public int sumCol(int col) {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[i][col];

}

return sum;

}

//---------------------------------------------------

//return the sum of the values in the main diagonal

//---------------------------------------------------

public int sumMainDiag() {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[i][i];

}

return sum;

}

//---------------------------------------------------------------

//return the sum of the values in the other ("reverse") diagonal

//---------------------------------------------------------------

public int sumOtherDiag() {

// Add your code here

int sum = 0;

for (int i = 0; i < square.length; i++) {

sum = sum + square[square.length - i - 1][i];

}

return sum;

}

//-------------------------------------------------------------------

//return true if the square is magic (all rows, cols, and diags have

//same sum), false otherwise

//-------------------------------------------------------------------

public boolean magic() {

// Add your code here. Check if the sum of main diagonal equals the other diagonal,

// also if all rows and all columns sums equal to the diagonal as well. Any uneuqal will

// terminate the comparison.

int d1 = sumMainDiag();

int d2 = sumOtherDiag();

if (d1 != d2) {

return false;

}

for (int i = 0; i < square.length; i++) {

if (d1 != sumRow(i) || d1 != sumCol(i)) {

return false;

}

}

return true;

}

//----------------------------------------------------

//read info into the square from the standard input.

//----------------------------------------------------

public void readSquare(Scanner scan) {

for (int row = 0; row < square.length; row++) {

for (int col = 0; col < square.length; col++) {

square[row][col] = scan.nextInt();

}

}

}

//---------------------------------------------------

//print the contents of the square, neatly formatted

//---------------------------------------------------

public void printSquare() {

for (int row = 0; row < square.length; row++) {

for (int col = 0; col < square.length; col++) {

System.out.print(square[row][col] + "\t");

}

System.out.println();

}

}

}

// ****************************************************************

// SquareTest.java

//

// Uses the Square class to read in square data and tell if

// each square is magic.

//

// ****************************************************************

class SquareTest {

public static void main(String[] args) throws IOException {

File file = new File("magicData.txt");

Scanner scan = new Scanner(file);

int count = 1; //count which square we're on

int size = scan.nextInt(); //size of next square

//Expecting -1 at bottom of input file

while (size != -1) {

//create a new Square of the given size

Square magicSquare = new Square(size);

//call its read method to read the values of the square

magicSquare.readSquare(scan);

System.out.println("\n******** Square " + count + " ********");

//print the square

magicSquare.printSquare();

//print the sums of its rows

for (int row = 0; row < size; row++) {

System.out.println("Sum of row " + row + ": "

+ magicSquare.sumRow(row));

}

//print the sums of its columns

for (int col = 0; col < size; col++) {

System.out.println("Sum of column " + col + ": "

+ magicSquare.sumCol(col));

}

//print the sum of the main diagonal

System.out.println("Sum of the main diagonal: "

+ magicSquare.sumMainDiag());

//print the sum of the other diagonal

System.out.println("Sum of the other diagonal: "

+ magicSquare.sumOtherDiag());

//determine and print whether it is a magic square

if (magicSquare.magic()) {

System.out.println("It's a magic square!");

} else {

System.out.println("It's not a magic square!");

}

System.out.println();

//get size of next square

size = scan.nextInt();

count++;

}

}

}


Related Questions

You have been asked to write a two-page report that explains the extent to which the IT department can configure the cryptographic features of Word 2010. What is the process involved in configuring encryption?
The scope of a variable declared inside of a function is:a) Local - within that functionb) Within that file onlyc) global
Find the quotient and the remainder of 11010111 ÷ 1010, assuming that the binary numbers are signed integers in two's complement form.
Which is an example of adaptive social behavior?
In cell B13, create a formula without a function using absolute references that subtracts the values of cells B5 andB7 from cell B6 and then multiples the result by cell B8. please help with excel!! I'm so lost

In open addressing with linear probing we must consider how to encodeA. Occupies positions

B. Available positions

C. All other answers

D.empty positions

Answers

Answer: B)Available positions

Explanation:Open addressing is the addressing method for the components that are present in the hash table. collision are controlled and managed by this process. The total count of keys is less or equal to the size of table.

Linear probing is the mechanism that helps in controlling of the collision happening by the process of key collection maintenance by encoding of the available positions of the element in the hash table.So,the correct option is option(B).

What suggestions do you have for preventing scope creep in projects?

Answers

Answer:

Scope creep is defined as the uncontrolled changes occur in the projects scope is known as scope creep. It basically occur when the project scope are not properly define and controlled.

Suggestions for preventing scope creep in projects are as follow:

  •   By using the online projects software and the software management help in prevent the scope creep in the projects.
  •   We should always focus on the projects requirements and must understand the main vision of the clients.
  •   There is no requirement of overdoing in projects rather keeping it simple and accurate according to the main requirements. otherwise, it may lead to scope creep.

Consider the following class definition.public class Tester
{
privtae int num1;
private int num2;
/missing constructor /
}
The following statement appears in a method in a class other than Tester. It is intended t o create a new Tester object t with its attributes set to 10 and 20.
Tester t = new Tester(10,20);

Which can be used to replace / missing constructor / so that the object t is correctly created?

Answers

Answer:

Explanation:

The following constructor needs to be added to the code so that the object called t can be created correctly...

public Tester(int arg1, int arg2) {

    num1 = arg1;

    num2 = arg2;

}

This basic constructor will allow the object to be created correctly and will take the two arguments passed to the object and apply them to the private int variables in the class. The question does not state what exactly the Tester constructor is supposed to accomplish so this is the basic format of what it needs to do for the object to be created correctly.

Define the function isEven which returns True, if a given number is even and returns False, if the number is odd. Execute the statement isEven(43) and display the output.

Answers

Answer:

Program :

#include <stdio.h> //header file

char* isEven(int number) //function

{

   if(number%2==0)

    return "True";

  else

   return "False";

}

int main() //mainfunction

{

   printf("%s",isEven(43)); //calling and display output.

   return 0; //return statement.

}

Output:

  • The above program gives as 45 output.

Explanation:

  • The above program is in c-language, The above function is used to check the number is even or odd.
  • If the number is even it returns true and if the number is odd, it returns false.
  • The print statement called that function while passing the number on the argument and print the result of the function.
  • The char* is used in return type because the function returns the string value.

If the following statement were in a C++ program, what would it do? cout >> "I love oranges and apples";

Answers

Answer:

It will show compilation error like - no match for 'operator>>'

Explanation:

<< and >> are input output stream operators which are used with different input output stream objects in C++. So << operator is used to output stream of characters to console or a file and >> operator is used to input or read some characters or integers from a file or console. Here input stream character is used with output stream object cout, which produces an error.  

How does an Index Organized table differ from a standard table and a typical index?

Answers

The thing is that an Index organized table, also known as IOT, is a type of table that stores data in a B tree index structure, which is used by Oracle. Normal relational tables, called heap-organized tables, store rows in any order and  primary key indexes store only the columns included in its definition.