mtn
MegaDork
12/7/10 8:48 p.m.
I can't figure this e35 m3 out. Its getting late, this is due tomorrow, and I have two finals tomorrow, a final on Thursday, and another program due on Friday. I need help:
How do I read from a file into a 2-dimensional array? For the life of me, I cannot figure out how to do it.
The problem I'm trying to figure out is here:
A magic square is a matrix that every row, column, and diagonal add up to the same thing, such as this one:
4 9 2
3 5 7
8 1 6
I need to write a class that will determine if any given square actually is or isn't a magic square. It needs to have two instance variables, an integer for the size of the square, and a 2-Dimensional array of integers that will make up the square.
The class needs to have two public methods; this is where I get stuck:
readSquare will take a filename and read in a magic square from the file. The first number in the file will indicate the size of the square (4 for a 4X4 with 16 numbers). The remainder will be the values to go in the square. So a file with a 3X3 square will have 10 values in it; one for a 4X4 square will have 17 numbers in it, etc. So the square referenced above would have a text file that has nothing but this in it:
3
4
9
2
3
5
7
8
1
6
And the last public method needs to check the square to see if it really is a magic square or not.
I also need four helper methods that return the sums of the values in each a specified row, specified column, and one for each of the diagonals, but I can get those on my own.
Thanks for any help,
Mike
EDIT - I apologize, GRM's forum code mangled my nicely formatted response.
Okay, so do you have your basic algorithms figured out?
The way that I would do this, and I'm not saying it's right for your class.
For the read:
+Open the file
+Read the first line
+Create a 2-dimensional array of the size given.
+Read in each line, putting it in the proper place... a nested for-loop would probably be ideal here.
+With the array full, CLOSE YOUR DAMN FILE. 
Now, for the scan, you basically just have to meet a set of conditions: all rows, columns, and diagonals must add up to the same number.
So, you take your 2-D array, and you add up the first row of the array and store that as your check value. Now you go through the rest of the rows and columns (again, nested for-loops are your friend) and check what they add up to against that first row - if any aren't equal, you know it's not a magic square, so you can just return false at that point. If all the checks pass, you "fall through" to your pass condition and return true.
Does that help at all?
mtn
MegaDork
12/7/10 10:27 p.m.
ReverendDexter wrote:
For the read:
+Open the file
+Read the first line
+Create a 2-dimensional array of the size given.
+Read in each line, putting it in the proper place... a nested for-loop would probably be ideal here.
+With the array full, CLOSE YOUR DAMN FILE.
This is where I can't figure it out. I need a for loop, I know that, but don't know how to set it up so that it reads the first line of the file as the size of the square, and then puts the next numbers into the square.
I actually barely know how to read from a file at all.
Open the file
read the first number into variable 'size'
allocate memory for your array array(x,y) based on the number read
for x=0; x < size; x++
for y=0;y<size; y++
{
read array(x,y)
}
mtn wrote:
I actually barely know how to read from a file at all.
Just so I'm not doing your homework for you... google:
- java streams
- java.io.Reader
- java.io.FileReader
It is all there with examples.
This is the most applicable the GRM forum titles have ever been, hahaha.
mtn
MegaDork
12/8/10 8:51 a.m.
ReverendDexter wrote:
This is the most applicable the GRM forum titles have ever been, hahaha.
Hahaha, yeah, well, I respect the broad scope of knowledge on the forum more than anyplace else and don't want to be told "Do a search" if I asked on a topic-specific forum. Hess and GPS, thanks.
And GPS, I've got most of the program finished... almost all of it, I just am having problems with the reading from a file, etc. I'm not looking for a freebee, I'm just looking for how to do it because the book and teacher aren't helping me.
Some of you may remember this thread:
http://grassrootsmotorsports.com/forum/off-topic-discussion/i-hate-programming/26877/page1/
As it turns out, there is a good chance I'll be retaking it anyways, but I am trying to figure it out. The teacher would be a really good teacher for an upper level programming class, but for an introduction class (or for an introduction non-major student), she is not. She asks questions to answer questions, trying to make me figure it out on my own, but I need her to show me how to do it which she won't. I'll do my own work, but I need to know how. And I figured out too late in the semester (yesterday) how much help a "Java for Dummies" book would have been.
And for anyone teaching java, don't use the book referenced in the previous thread. It is awful, it teaches you how to use a particular class of java and not java itself.
The simplest possible example...
FileReader fr = new FileReader("somefile");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
int r = Integer.parse(s);
// put it somewhere
}
fr.close();