Page 428 - Computer science 868 Class 12
P. 428

3.  Write a recursive method to generate a magic square of odd size. A magic square is a matrix where sum of rows, columns and
                    diagonals are all equal to size x (size  - 1)/2.
                                                2
                     The Siam method to generate a magic square of odd order is as follows:
                    •  Place 1 in top row middle column.
                    •  The next element will be placed diagonally up i.e., move 1 row up and 1 column right.
                    •  If row position becomes -1, the number is placed straight down to the corresponding cell in the bottom row.
                    •  If column position becomes equal to size, the number is placed across to the corresponding cell on the left side of our square.
                    •  If both row position becomes -1 and column position equal to size, then the number is placed  to the empty cell directly below
                      the last number entered.
                    •  If the cell where the number is to be stored is not vacant, then the number is placed  to the empty cell directly below the last
                      number entered.
                    •  This process is repeated for size 2 number of elements to fill the entire square.
                     Example: if size is 5 then the magic square will be

                                                      17     24    1      8     15
                                                      23     5     7     14     16
                                                       4     6     13    20     22

                                                      10     12    19    21     3
                                                      11     18    25     2     9

                Ans.  import java.util.*;
                    class Magicsq
                    { int size;
                     int a[][];
                     Magicsq(int s) // default constructor
                     { size=s;
                      a=new int[size][size];
                      for(int i=0;i<size;i++)
                      { for(int j=0;j<size;j++)
                       { a[i][j]=0;}
                      }
                      }
                     void generate(int row,int col,int n)
                     { if(n>size*size) // base case
                       System.out.println();
                       else
                       {
                       if(row==-1 && col==size) // if both row & column is beyond array range
                       { row=row+2;
                        col=col-1;
                       }
                       else if(row==-1) // if row<0
                       { row=size-1;}
                       else if(col==size) // column=size
                       { col=0;}
                       else if(a[row][col]!=0) // if not empty
                       {row=row+2;



                426426  Touchpad Computer Science-XII
   423   424   425   426   427   428   429   430   431   432   433