For Problem 3 you can use 1 or 2 extra arrays of at most n e

For Problem 3 you can use 1 or 2 extra arrays of at most n elements each, or 1 array with at most 2*n elements, and a few extra variables.

Use comments to explain well your “algorithms” to solve the given problem and an estimate of how many operations (such as comparisons, swaps, additions etc.) your algorithm performs

MAGIC SQUARE A magic square is a square array of numbers 4 9 2 consisting of the distinct positive integers 1, n2 arranged such that the sum of the n 3 5 7 numbers in any horizontal, vertical, or main 8 1 6 diagonal line is always the same number known as the magic constant 1 n On: 1 16 3 13 So for n-3 the magic constant is 15, while 5 10 11 8 for n 4 it has the value 34. 9 6 7 12 Given a square array, write a program to check if the array is a Magic Square. 4 15 14 1

Solution

Hi, Please find my program.

Please let me knwo in case of any issue.

public class MagicSquare {

   private int a[][];

   public MagicSquare(int arr[][]) {

       this.a = arr;

   }

   // function to print matrix

   public void showArray(){

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

           for(int j=0; j<a[0].length; j++){

               System.out.print(a[i][j]+\" \");

           }

           System.out.println();

       }

   }

   // function to check whether satisfies all conditions to be Magic Square

   private boolean check(){

       int sum,s;

       sum=0;

       for (int col = 0; col< a[0].length; col++) //first set the sum

           sum += a[0][col];

       //check the next rows

       s = 0;

       for (int row = 1; row < a.length; row++){

           s = 0;

           for (int col = 0; col< a[0].length; col++)

               s += a[row][col];

           if (s != sum)

               return false;

       }

       //check columns

       for (int col = 0; col< a[0].length; col++) {

           s = 0;

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

               s += a[row][col];

           }

           if (s != sum)

               return false;

       }

       //check diagonal 1

       s =0;

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

           s += a[row][row];

       }

       if (s != sum)

           return false;

       //check diagonal 2

       s =0;

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

           s += a[row][2 - row];

       }

       if (s != sum)

           return false;

       /*//check that the numbers 1..9 appear

       //We use an array of booleans and mark each one as we find it.

       boolean[] appears = new boolean[9];

       for (int i=0; i < 9; i++){

           appears[i] = false;

       }

       */

       //Check each one

       /*for (int col = 0; col< a[0].length; col++) {

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

               int num = a[row][col];

               if (appears[num-1]) //its easy to forget that -1.

                   return false; //oops, this number appears twice!

               appears[num-1] = true;

           }

       }

       */

       //I could check here that all the values in appears are true but

       // since there are 9 positions in both appears and a[][], I know

       // that if we get to this point then all the values in appears[]

       // must be true!

      

       return true;

   }

   //showResult

   public void showResult(){

       if(check())

           System.out.println(\"Matrix is Magic Square\");

       else

           System.out.println(\"Matrix is not Magic Square\");

   }

}

//Test class

public class MagicSquareTest{

   public static void main (String[] args){

       //Test 1

       int[][] loshu = {{4,9,2},{3,5,7},{8,1,6}};

       MagicSquare ms1 = new MagicSquare(loshu);

       ms1.showArray();

       ms1.showResult();

       System.out.println();

       int[][] no1 = {{4,9,2},{3,7,5},{8,1,6}};

       MagicSquare ms2 = new MagicSquare(no1);

       ms2.showArray();

       ms2.showResult();

       int[][] no2 = {{4,9,2},{3,3,5},{8,1,6}};

       System.out.println();

       MagicSquare ms3 = new MagicSquare(no2);

       ms3.showArray();

       ms3.showResult();

   }

}

/*

Sample run:

4 9 2

3 5 7

8 1 6

Matrix is Magic Square

4 9 2

3 7 5

8 1 6

Matrix is not Magic Square

4 9 2

3 3 5

8 1 6

Matrix is not Magic Square

*/

For Problem 3 you can use 1 or 2 extra arrays of at most n elements each, or 1 array with at most 2*n elements, and a few extra variables. Use comments to expla
For Problem 3 you can use 1 or 2 extra arrays of at most n elements each, or 1 array with at most 2*n elements, and a few extra variables. Use comments to expla
For Problem 3 you can use 1 or 2 extra arrays of at most n elements each, or 1 array with at most 2*n elements, and a few extra variables. Use comments to expla
For Problem 3 you can use 1 or 2 extra arrays of at most n elements each, or 1 array with at most 2*n elements, and a few extra variables. Use comments to expla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site