MagicSquare program not right.

I wrote this but cannot get it to work properly

Page 1 of 1

3 Replies - 1213 Views - Last Post: 22 July 2007 - 10:49 AM Rate Topic: -----

#1 KoreyAusTex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-July 07

MagicSquare program not right.

Post icon  Posted 21 July 2007 - 07:50 PM

I cannot get this Magic Square verifying program to work correctly and I think the problem lies in the last part of the IsMagic method, where after the rows ,columns and diagonals are added up it would then have to use a boolean method to check. Any suggestions, I started writing this yesterday and have not been able to make significant headway on it. Please offer suggestions as to how I might do better because this is pretty hard for me.

 
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++;
			}
			
			if(true)
			{
				System.out.println('\n' + "This array is a magic square");
			}
			else System.out.println('\n' + "This array is not a magic square");
		}
	}
	
   
	
	public static void main (String[] args)
	{
		int[][] noItIsntArray = {{2, 3, 6}, {4, 3, 6}, {4, 3, 6}};
		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(noItIsntArray);
		isMagic(xArray);
		isMagic(yArray);
		isMagic(zArray);
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: MagicSquare program not right.

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: MagicSquare program not right.

Posted 21 July 2007 - 11:52 PM

the diagonals would be a sum of the form:
				for (int j= 0; j< b[0].length; j++)
				{
					sum= sum+b[j][j];
				}



note that it is not a nested loop.
The other diagonal would look something like b[b.length-1-j][j]
Was This Post Helpful? 0
  • +
  • -

#3 KoreyAusTex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-July 07

Re: MagicSquare program not right.

Posted 22 July 2007 - 01:13 AM

View PostNickDMax, on 21 Jul, 2007 - 11:52 PM, said:

the diagonals would be a sum of the form:
				for (int j= 0; j< b[0].length; j++)
				{
					sum= sum+b[j][j];
				}



note that it is not a nested loop.
The other diagonal would look something like b[b.length-1-j][j]


Actually the rows add up fine I am just having problems with the program telling me if the matrix is a magic square, thanks for your help! I do appreciate it.

[quote name='KoreyAusTex' date='22 Jul, 2007 - 01:10 AM' post='238963']
[quote name='NickDMax' post='238945' date='21 Jul, 2007 - 11:52 PM']
the diagonals would be a sum of the form:
			if(true)
			{
				System.out.println('\n' + "This array is a magic square");
			}
			else System.out.println('\n' + "This array is not a magic square");
		}
	}



This is the problem, I cannot figure out how to get the program to verify if it is or isn't a Magic Square. I think I might be missing a boolean statement.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: MagicSquare program not right.

Posted 22 July 2007 - 10:49 AM

Well, lets see: You will need to calculate the "magic number".
Then for each row/column/diagonal you will need to check if the sum is equal to the Magic sum.

OR

There really is no need to calculate the magic sum, since all you need to know is if all the sums are equal. So just keep a variable like FirstSum which holds the first sum you calculate and compare it to the sum of a given row/column/diagonal and if you find one that is not equal you know you don't have a magic square.

BTW, I HATE to rain on your parade, but I am pretty sure that your sums for the diagonals are not logically correct.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1