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

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.
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with random integers between a given min and max value. It must accept for parameters, min and max values for the creation of random integers, and rows and columns for the number of rows and columns of the array. The method should return an array of rows by columns size, filled with random integers between min and max values. The second method is called printRA,...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. The Lo Shu Magic Square has the following properties:
Python programmingThe Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. 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 as shown in figure.In a program you can simulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT