Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [SOLVED] [Java/C++] Magic Squares  (Read 6001 times)

0 Members and 1 Guest are viewing this topic.
[SOLVED] [Java/C++] Magic Squares
« on: November 23, 2010, 10:55:18 pm »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1767
So I have this project to do:


The thing is, I'm not really sure about how to go about doing it. The thing that confuses me the most I suppose would be going finding out whether or not the amount of values entered is the square of some number efficiently. Also, how to go about organizing the array to test whether or not it's magic, should be a two-dimensional or one-dimensional array. Anybody got any suggestions?
« Last Edit: November 24, 2010, 02:34:14 am by Colbydude »
Logged
  • https://colbydude.com

Xiphirx

wat
Re: [Java/C++] Magic Squares
« Reply #1 on: November 23, 2010, 10:57:25 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
Ah, this brings back memories from my first programming course :)

I did this with C++, I'll post back here if I find the source code.
Logged
  • For The Swarm
Re: [Java/C++] Magic Squares
« Reply #2 on: November 23, 2010, 11:31:20 pm »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
That doesn't seem that difficult; for some reason it makes me think of tokenizing a user typed-in string, looking through the resulting substrings to see which are numbers (and then putting those numbers into an array), etc. That assumes you aren't assuming every passed in value is a number, or that you aren't doing it by having something like an infinite loop that terminates with some escape value. If you are doing the cin stuff, you can test to see if it is a number and then if it is add it in. The rest of the calculation is just knowing if the number of numbers is a valid square or not.

hen you could just do for loops to figure out the row, column, and diagonal sums (well the diagonal sums could be done in the same loop). Knowing if a number is repeated is also easy to do; I forget the efficient way of doing that though. Like, I would think that the list of numbers entered isn't going to be !@#$% huge so the list of numbers doesn't have to be sorted with some technique or if the numbers themselves would be best put into a data structure upon being read in (red-black trees up in this !@#$%). A two-dimensional array can be expressed as a one-dimension one.

You can do the problem, all that crap I mentioned above, especially the second paragraph just makes it look worse. If you are doing this in Java it might be more of a cakewalk though.
Logged

Xiphirx

wat
Re: [Java/C++] Magic Squares
« Reply #3 on: November 24, 2010, 12:04:31 am »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
Unfortunately I did not find the source code. I'm a bit busy today, but I'll help you out tomorrow.
Logged
  • For The Swarm
Re: [Java/C++] Magic Squares
« Reply #4 on: November 24, 2010, 12:24:16 am »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1767
Unfortunately I did not find the source code. I'm a bit busy today, but I'll help you out tomorrow.

Haha unfortunately it's due tomorrow morning.

Luckily though I seem to be progressing. I'll post back whether I get it or not.
Logged
  • https://colbydude.com
Re: [Java/C++] Magic Squares
« Reply #5 on: November 24, 2010, 01:11:06 am »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1767
Alright good news. I think I got everything figured out in what I need to do.

Here's what I got so far:

MagicTester.java:
Code: [Select]
import java.util.Scanner;
import java.util.ArrayList;
public class MagicTester
{
// Main.
public static void main(String[] args)
{
// Construct the input array list.
ArrayList<Integer> input = new ArrayList<Integer>();
// Construct a scanner.
Scanner in = new Scanner(System.in);
// Prompt for n^2.
System.out.print("Enter an n^2 value (or input 0 to quit): ");
int n = in.nextInt();
if (n != 0)
input.add(n);
// Prompt for more until the user enters 0.
while (n != 0)
{
System.out.print("Enter another n^2 value (or input 0 to quit): ");
n = in.nextInt();
if (n != 0)
input.add(n);
}
// Construct MagicSquare object.
MagicSquare square = new MagicSquare(input.size());
// Add all the input values into the magic square.
for (Integer i : input)
square.add(i);
// Test to see if it's a magic square.
if (square.isMagic())
System.out.println("Yes the values entered create a magic square!");
else
System.out.println("Unfortunately the values entered do not create a magic square...");
}
}

MagicSquare.java:
Code: [Select]
public class MagicSquare
{
private int[] values;
private int index;
private int magicn;

public MagicSquare(int size)
{
values = new int[size];
index = 0;
magicn = 0;
}

public void add(int i)
{
values[index] = i;
index++;
}

public boolean isMagic()
{
System.out.println(values.length);
// Check to make sure that the length is the square of a number in the array.
for (int i = 0; i < values.length; i++)
{
if (i * i == values.length)
{
magicn = i;
break;
}
}
if (magicn == 0)
return false;
System.out.println("Magic Number is " + magicn);
// Check to make sure every number between 1 and the length exists in the array.
for (int i = 0; i < values.length; i++)
{
if (checkExists(i + 1) == false)
return false;
}
// Lastly find out if all sides and diagnols add up.
return true;
}

public boolean checkExists(int i)
{
for (int k = 0; k < values.length; k++)
{
if (values[k] == i)
return true;
}
return false;
}
}

If anybody has any suggestions as to making it more efficient, have a go at it. I'll start throwing in the calculations on the sides and diagonals now.
(Also ignore the prompt messages, I haven't changed them from what I had originally yet.)
Logged
  • https://colbydude.com
Re: [Java/C++] Magic Squares
« Reply #6 on: November 24, 2010, 02:33:42 am »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1767
Sorry for the triple post, but SUCCESS! Took a while, but I managed to get it done.

Here's the source code for those who may ever need it for whatever reason.

MagicTester.java:
Code: [Select]
// Import necessary classes.
import java.util.Scanner;
import java.util.ArrayList;
// Public Class Magic Tester
// Prompts the user for values and tests whether they form a magic square.
public class MagicTester
{
// Main.
public static void main(String[] args)
{
// Construct the input array list.
ArrayList<Integer> input = new ArrayList<Integer>();
// Construct a scanner.
Scanner in = new Scanner(System.in);
// Prompt for n^2.
System.out.print("Enter an n^2 value (or input 0 to quit): ");
int n = in.nextInt();
if (n != 0)
input.add(n);
// Prompt for more until the user enters 0.
while (n != 0)
{
System.out.print("Enter another n^2 value (or input 0 to quit): ");
n = in.nextInt();
if (n != 0)
input.add(n);
}
// Construct MagicSquare object.
MagicSquare square = new MagicSquare(input.size());
// Add all the input values into the magic square.
for (Integer i : input)
square.add(i);
// Test to see if it's a magic square.
if (square.isMagic())
System.out.println("Yes the values entered create a magic square!");
else
System.out.println("Unfortunately the values entered do not create a magic square...");
}
}

MagicSquare.java:
Code: [Select]
// Public Class Magic Square.
// Class that holds input values and determines if it is a
// "magic square."
public class MagicSquare
{
private int[] values; // The input values.
private int index; // Index to keep track of the array.
private int magicn; // Magic Number.
private int magicsum; // Magic Sum.

// Constructor.
public MagicSquare(int size)
{
values = new int[size];
index = 0;
magicn = 0;
}

// Adds the value to the array.
public void add(int i)
{
values[index] = i;
index++;
}

// Returns whether or not the square is "magic."
public boolean isMagic()
{
System.out.println(values.length);
// Check to make sure that the length is the square of a number in the array.
for (int i = 0; i < values.length; i++)
{
if (i * i == values.length)
{
magicn = i;
break;
}
}
if (magicn == 0)
{
System.out.println("Magic Number is absent, or the amount of values is not a square value.");
return false;
}
// Check to make sure every number between 1 and the length exists in the array.
for (int i = 0; i < values.length; i++)
{
if (checkExists(i + 1) == false)
{
System.out.println("The number " + (i + 1) + " was missing.");
return false;
}
}
// Lastly find out if all sides and diagnols add up.
// Top Row/Get the "magic sum."
for (int i = 0; i < magicn; i++)
magicsum = magicsum + values[i];
// Bottom Row
int magicsumtwo = 0;
for (int i = 0; i < magicn; i++)
magicsumtwo = magicsumtwo + values[i + (magicn * (magicn - 1))];
if (magicsumtwo != magicsum)
return false;
// Left Column
magicsumtwo = 0;
for (int i = 0; i < magicn; i++)
magicsumtwo = magicsumtwo + values[0 + (magicn * i)];
if (magicsumtwo != magicsum)
return false;
// Right Column
magicsumtwo = 0;
for (int i = 0; i < magicn; i++)
magicsumtwo = magicsumtwo + values[(i * magicn) + (magicn - 1)];
if (magicsumtwo != magicsum)
return false;
// Diagonal going down-right
magicsumtwo = 0;
for (int i = 0; i < magicn; i++)
magicsumtwo = magicsumtwo + values[(i * magicn) + (i)];
if (magicsumtwo != magicsum)
return false;
// Diagonal going down-left
magicsumtwo = 0;
for (int i = 0; i < magicn; i++)
magicsumtwo = magicsumtwo + values[(i * magicn) + (magicn - i - 1)];
if (magicsumtwo != magicsum)
return false;
// Everything checks out so it's a magic square.
return true;
}

// Makes it easier to check if a value is in the array.
public boolean checkExists(int i)
{
for (int k = 0; k < values.length; k++)
{
if (values[k] == i)
return true;
}
return false;
}
}

Thanks for the help guys.
Logged
  • https://colbydude.com

Xiphirx

wat
Re: [SOLVED] [Java/C++] Magic Squares
« Reply #7 on: November 24, 2010, 02:38:45 am »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
Heh, was just about to post here. I found a file named "MagicSquare.cpp", but it contains a custom string class I had to make for my class .... I really don't know why its named MagicSquare.cpp... D:
Logged
  • For The Swarm
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.284 seconds with 53 queries.

anything