Magic Square program run amuck!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KoreyAusTex
    New Member
    • Feb 2007
    • 36

    Magic Square program run amuck!

    I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong:

    Code:
    import java.util.*;
    
    public class MagicSquare
    {
        public static void isMagic (int[][] b)
        {
           int sum = 0;
           System.out.print('\n' + "Sum of Columns: ");
           for (int i =0 ; i < b.length; i++)
            {
            for (int j= 0; j< b[i].length; j++)
            {
                sum= sum+b[0][j];
            }
                System.out.print(sum + " ");
                sum = 0;
            }
            System.out.println(" ");
            System.out.print("Sum of Rows: ");
            int x =0;
           while (x < b.length)
           {
           for (int i =0 ; i < b.length; i++)
            {
                sum= sum + b[i][x];
            }
                System.out.print(sum + " ");
                sum=0;
                x++;
            }
            {
                System.out.println(" ");
                System.out.print("Sum of Diagonals: ");
                for (int i =0 ; i < b.length; i++)
                {
                    for (int j= 0; j< b[i].length; j++)
                    {
                        sum= sum+b[0][j];
                    }
                    
                    System.out.print(sum + " ");
                    sum = 0;
                    i++;
                }      
            }
        }
        
        // returns true if sq is a magic square 
        boolean isMagicTest(int [][] b, int[][] xArray) 
        {
            int sum = 0;// the sum that all rows, columns, and diags should equal
            for (int i = 0; i < b.length; i++) 
            { 
                sum += b[i][0]; 
            }
                
            int diag1 = 0;
            int diag2 = 0; // the sums of the two diagonals
            for (int i = 0; i < b.length; i++)
            {
                int hor = 0, vert = 0; // the sums of the current row and column
                for (int j = 0; j < b[i].length; j++)
                {
                    if (b[i][j] == 0) return false;
                    hor += b[i][j];
                    vert += b[j][i];
                }
                if (hor != sum || vert != sum) 
                    return false;
                diag1 += b[i][i];
                diag2 += b[2-i][i];
             }
             if (diag1 != sum || diag2 != sum) 
                //return false;
                System.out.println("This is not a Magic square!");
                return false;
             // no failures found, so we have a magic square!
             //return true;
             System.out.println("This is a Magic square!");
             return true;
            }   
        
        public static void main (String[] args)
        {
            int[][] xArray = {{5, 9, 1}, {3, 4, 8}, {7, 2, 6}};
            int[][] yArray = {{1, 3, 16, 14}, {8, 15, 2, 9},
            {13, 6, 11, 4}, {12, 10, 5, 7}};
            int[][] zArray = {{18, 24, 5, 6, 12}, {10, 11, 17, 23, 4},
                 {22, 3, 9, 15, 16}, {14, 20, 21, 2, 8},
                         {1, 7, 13, 19, 25}};
            isMagic(xArray);
            //isMagic(yArray);
            //isMagic(zArray);
            
        }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    edit: all the spoonfeeding by me deleted; the OP got all the help already in
    Sun's Java forum but choose to ignore it.

    Jos

    Comment

    • prometheuzz
      Recognized Expert New Member
      • Apr 2007
      • 197

      #3
      Originally posted by KoreyAusTex
      I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong:

      ...
      Why did you not reply to the reactions in your thread at Sun's forum?


      You're a time waster!

      Comment

      • KoreyAusTex
        New Member
        • Feb 2007
        • 36

        #4
        Originally posted by prometheuzz
        Why did you not reply to the reactions in your thread at Sun's forum?


        You're a time waster!
        Because I am not sure what you are talking about, I just wrote this program last night and today, I looked at the link but I am not sure what you mean? How am I a timewaster?

        Comment

        • KoreyAusTex
          New Member
          • Feb 2007
          • 36

          #5
          can someone please help??

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by KoreyAusTex
            Because I am not sure what you are talking about, I just wrote this program last night and today, I looked at the link but I am not sure what you mean? How am I a timewaster?
            I compared the source code in this thread with the source code in the thread
            mentioned by Prometheuzz: most of the erroneous logic is the same, most of
            the variable names are the same and the test magic triangles are the same.

            If conclude that both source code texts were written by you; you received good
            help in that other forum. You decided to post your question here again. Why?
            Our answer would have been the same as you got in that other forum. Even the
            same people hang around in both the forums as you can see.

            kind regards,

            Jos

            Comment

            • KoreyAusTex
              New Member
              • Feb 2007
              • 36

              #7
              Originally posted by JosAH
              I compared the source code in this thread with the source code in the thread
              mentioned by Prometheuzz: most of the erroneous logic is the same, most of
              the variable names are the same and the test magic triangles are the same.

              If conclude that both source code texts were written by you; you received good
              help in that other forum. You decided to post your question here again. Why?
              Our answer would have been the same as you got in that other forum. Even the
              same people hang around in both the forums as you can see.

              kind regards,

              Jos

              By the way why don't you get in touch with the other kid and ask him, you talk to him and I can guarantee YOU ARE WRONG.

              Comment

              • prometheuzz
                Recognized Expert New Member
                • Apr 2007
                • 197

                #8
                Originally posted by KoreyAusTex
                By the way why don't you get in touch with the other kid and ask him, you talk to him and I can guarantee YOU ARE WRONG.
                Ok, that could be true. And if it is, I apologize. You have to understand that this does happen quite a lot and that it is frustrating if you spend time answering someone but that person doesn't even look at it because he posted the same thing else where.
                But you needn't have made such a big fuss about it: I merely said you were waisting other people's time (if it was you indeed who cross posted this), which is IMO not such a big deal. If you had only explained yourself like you explained yourself in the feedback message all would be OK, and I'm sure you would have received some good advice. Unfortunately you also used quite some foul language in your feedback response which does not help you a lot. Perhaps someone (other than me) would like to help you on this.

                Good luck.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Ok, I'll help you out a bit then, but there'll be less spoonfeeding; suppose you
                  start at a postion x,y in a square matrix. This x,y position is at the border of
                  the matrix. Also suppose you 'walk' in the direction dx, dy; you can find the sum
                  of the numers on that traversal walk like this:

                  [code=java]
                  int sum(int b[][], int x, int y, int dx, int dy) {
                  int sum= 0;
                  for (int i= 0; i < b.length; i++, x+= dx, y+= dy)
                  sum+= b[x][y];
                  return sum;
                  }
                  [/code]

                  If you understand this little method you'll also understand that it can compute
                  sums of rows, sums of columns and the sums of the two diagonals. All you
                  have to do is feed it the correct x, y, dx and dy values.

                  Check whether or not those sums are equal. If they are the square is a magic square.

                  kind regards,

                  Jos

                  Comment

                  Working...