Question

In: Computer Science

You will put a possible magic square into a two dimensional array and determine if it...

You will put a possible magic square into a two dimensional array and determine if it is a magic square or not. Some of the code is completed. You can assume that it is a square array and that all of the integers are unique (no repeats)

CODE:

package TwoDimensionalArrays;

public class MagicSquare {
   public static boolean checkMagicSquare(int[][]array)
   {   //Pre: Assume that the array is a square two dimensional array
       //Pre: Assume that there are no repeat integers
       int sum=0;
       for(int col=0;col<array[0].length;col++)
       {
           sum=sum+array[0][col];
       }
       //sum will represent the sum of every row, every col and the diagonals
       if(checkRows(array,sum)&&checkCols(array,sum)&&checkDiagonals(array,sum))
           return true;
       else
           return false;
   }
   public static boolean checkRows(int[][]a,int sum)
   {
      
       for(int row=0;row<a.length;row++)
       {
           int rowSum=0;
           for(int col=0;col<a[0].length;col++) {
               rowSum+=a[row][col];
           }//end column loop
           if(rowSum!=sum)
               return false;   //This will jump out of the method and return false if not magic square
       }//end row loop
      
       return true;             //The rows all add up to the same value
   }
   public static boolean checkCols(int[][]a,int sum)
   {
      
      
       return true;             //The rows all add up to the same value
   }
   public static boolean checkDiagonals(int[][]a,int sum)
   {   int leftDiagonalSum=0;
       int rightDiagonalSum=0;
      


      
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int[][]array1= {{6,1,8},{7,5,3},{2,9,4}};
       int[][]array2= {{9,1,5},{3,4,8},{6,7,2}};
       System.out.println(checkMagicSquare(array1));
       System.out.println(checkMagicSquare(array2));
   }

}
/*
true
false
*/

Solutions

Expert Solution

CODE:


public class MagicSquare{
public static boolean checkMagicSquare(int[][]array)
{ //Pre: Assume that the array is a square two dimensional array
//Pre: Assume that there are no repeat integers
int sum=0;
for(int col=0;col<array[0].length;col++)
{
sum=sum+array[0][col];
}
//sum will represent the sum of every row, every col and the diagonals
if(checkRows(array,sum)&&checkCols(array,sum)&&checkDiagonals(array,sum))
return true;
else
return false;
}
public static boolean checkRows(int[][]a,int sum)
{
  
for(int row=0;row<a.length;row++)
{
int rowSum=0;
for(int col=0;col<a[0].length;col++) {
rowSum+=a[row][col];
}//end column loop
if(rowSum!=sum)
return false; //This will jump out of the method and return false if not magic square
}//end row loop
  
return true; //The rows all add up to the same value
}
public static boolean checkCols(int[][]a,int sum)
{
for(int col=0;col<a[0].length;col++)
{
int colSum=0;
for(int row=0;row<a.length;row++) {
colSum+=a[row][col];
}//end row loop
if(colSum!=sum)
return false; //This will jump out of the method and return false if not magic square
}//end col loop
  
return true; //The volumns all add up to the same value
}
public static boolean checkDiagonals(int[][]a,int sum)
{ int leftDiagonalSum=0;
int rightDiagonalSum=0;
  
for(int row=0;row<a.length;row++)
{
int rowSum=0;
for(int col=0;col<a[0].length;col++) {
if(row == col)
{
leftDiagonalSum +=a[row][col];
}
if(row + col == a.length - 1)
{
rightDiagonalSum += a[row][col];
}

}//end column loop
  
}//end row loop
  
return leftDiagonalSum == sum && rightDiagonalSum == sum;
  
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][]array1= {{6,1,8},{7,5,3},{2,9,4}};
int[][]array2= {{9,1,5},{3,4,8},{6,7,2}};
System.out.println(checkMagicSquare(array1));
System.out.println(checkMagicSquare(array2));
}

}
OUTPUT:


Related Solutions

you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
- Students must develop the code required to create a square two-dimensional array just large enough...
- Students must develop the code required to create a square two-dimensional array just large enough to store a given string in encrypted form using the given encryption algorithm. The character used after the end of the encrypted string is the ASCII delete key, set as final UNPRINTABLE_CHAR_VALUE. After this value is stored, the encryption process fills the remaining elements with random values - Students must develop the code to decrypt a given square two-dimensional array and using the given...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figures 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 in Figure B. In a program you can stimulate a magic square using a two-dimensional list. Write a function...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
Build a two dimensional array out of the following three lists. The array will represent a...
Build a two dimensional array out of the following three lists. The array will represent a deck of cards. The values in dCardValues correspond to the card names in dCardNames. Note that when you make an array all data types must be the same. Apply dSuits to dCardValues and dCardNames by assigning a suit to each set of 13 elements. dCardNames = ['2','3','4','5','6','7','8','9','10','J','Q','K','A'] dCardValues = ['2','3','4','5','6','7','8','9','10','11','12','13','14'] dSuits = ["Clubs","Spades","Diamonds","Hearts"] Once assigned your two dimensional array should resemble this : 2...
1.Declare a two-dimensional array of Strings namedchessboard.
1. Declare a two-dimensional array of Strings named chessboard.2. Declare a two-dimensional array of integers named tictactoe.3. Declare and create a two-dimensional array of chars,tictactoe, with 3 rows, each with 3 elements.4. Create a two-dimensional array of ints, plan, with 2 rows, and and 3 columns and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75. Use member initializer syntax.
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the...
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the lab grades, quiz grades and exam grades of three students in the professor’s class. Write a C++ program that calculate the final grade for each student and save it to the last column. You program should display the following output:             Lab    Quiz      Exam       Final     Student 1          ## ## ## ## Student 2          ## ## ## ## Student 3          ## ## ## ## The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT