Results 1 to 6 of 6
Thread: Java Task with matrix.
- 10-16-2012, 05:15 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
Java Task with matrix.
Hi, every one! Please help me with my task. I don't know which way to think. Thanks!!!
Given a matrix of elements equal to 0 or 1. Units in such a matrix form
domains, and the domain contains only the neighboring elements in the horizontal and
vertical. The program should determine the number of domains in a given matrix.
input
A string containing the path to the file containing the matrix.
Example of a matrix:
1 0 0 0 1 0
1 1 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1
This matrix can be determined 4 different domain:
{(1,1), (2,1), (2,2)}, {(1,5)}, {(2,6)}, {(4,6), (5,6)}.
- 10-16-2012, 11:20 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Java Task with matrix.
...and? What have you tried? Where exactly are you stuck? Contributors won't do this for you, but will point you in the right direction. Without knowing the answers to those question, we have no clue which direction to pointHi, every one! Please help me with my task. I don't know which way to think
- 10-24-2012, 12:39 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
Re: Java Task with matrix.
Hi, every one! I written code for reading this maxrix from file and displaying all coordinats of ones in matrix. But when I try display only ones without the neighboring elements in the horizontal and vertical:
1 0 0 0 1 0
1 1 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1
And I have got exception - ArrayIndexOutOfBoundsException, because I exceed the length of the array. How to fix it? Thanks!
Java Code:public class Matrix { public static int a; public static void main(String[] args) throws IOException { File file = new File("src\\array.txt"); int row = 0; int col = 0; int[][] data = new int[5][6]; BufferedReader bufRdr = new BufferedReader(new FileReader(file)); String line = null; while ((line = bufRdr.readLine()) != null && row < data.length) { StringTokenizer st = new StringTokenizer(line, " "); while (st.hasMoreTokens()) { data[row][col] = Integer.parseInt(st.nextToken()); col++; } col = 0; row++; }; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { int nextRow = i + 1; int nextCol = j + 1; if (data[i][j] == 1 && data[nextRow][j]!=1 && data[i][nextCol]!=1) { System.out.println("(" + i + "," + j + ")"); } } }; }; };Last edited by vitaliy1901; 10-25-2012 at 11:32 AM.
- 10-25-2012, 08:12 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Task with matrix.
A flood fill mechanism can do the job; i.e. if a point (x,y) is part of a domain, recursively check its neighbouring points. Check for array bounds when finding the neighbours. The stop criterium is easy: if (x,y) == 0, it is not part of the domain under construction, else if (x,y) == 1 and it is already part of the domain under construction, then stop, else add (x,y) to the new domain and recursively check its neighbours again.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-25-2012, 03:33 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
Re: Java Task with matrix.
Hi Jos! Thanks for the help, It's work.
Java Code:public class Matrix { private static int quantity; public static void main(String[] args) throws IOException { for (int i = 0; i < matrix().length - 1; i++) { for (int j = 0; j < matrix()[i].length - 1; j++) { int nextRow = i + 1; int nextCol = j + 1; int nextNextRow = i + 2; int nextNextCol = j + 2; boolean three0111 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1; boolean three1011 = matrix()[i][j] == 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1; boolean three1101 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] == 1; boolean three1110 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1; boolean threeRight1110 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[i][nextNextCol] == 1 && matrix()[nextRow][j] != 1; boolean threeDown1110 = matrix()[i][j] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextNextRow][j] == 1 && matrix()[i][nextCol] != 1; boolean one0100 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] != 1; boolean one0110 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1; boolean two0101 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] == 1; boolean two1010 = matrix()[i][j] == 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1; boolean two0011 = matrix()[i][j] != 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1; if (three0111) { System.out.print("(" + i + "," + j + ")"); System.out.print("(" + nextRow + "," + nextCol + ")"); System.out.print("(" + nextRow + "," + j + ")"); System.out.print(" || "); quantity++; } else if (three1011) { System.out.print("(" + i + "," + j + ")"); System.out.print("(" + nextRow + "," + nextCol + ")"); System.out.print("(" + nextRow + "," + j + ")"); System.out.print(" || "); quantity++; } else if (three1101) { System.out.print("(" + i + "," + j + ")"); System.out.print("(" + nextRow + "," + nextCol + ")"); System.out.print("(" + nextRow + "," + j + ")"); System.out.print(" || "); quantity++; } else if (three1110) { System.out.print("(" + i + "," + j + ")"); System.out.print("(" + nextRow + "," + nextCol + ")"); System.out.print("(" + nextRow + "," + j + ")"); System.out.print(" || "); quantity++; } else if (threeRight1110) { System.out.print("(" + i + "," + j + ")"); System.out.print("(" + i + "," + nextCol + ")"); System.out.print("(" + i + "," + nextNextCol + ")"); System.out.print(" || "); quantity++; } else if (threeDown1110) { System.out.print("(" + i + "," + j + ")"); System.out.print("(" + nextRow + "," + j + ")"); System.out.print("(" + nextNextRow + "," + j + ")"); System.out.print(" || "); quantity++; } else if (one0100) { System.out.print("(" + i + "," + nextCol + ")"); System.out.print(" || "); quantity++; }else if (one0110) { System.out.print("(" + i + "," + nextCol + ")"); System.out.print(" || "); quantity++; } else if (two0101) { System.out.print("(" + i + "," + nextCol + ")"); System.out.print("(" + nextRow + "," + nextCol + ")"); quantity++; } else if (two1010) { System.out.print("(" + i + "," + j + ")"); System.out.print("(" + nextRow + "," + j + ")"); quantity++; }else if (two0011) { System.out.print("(" + nextRow + "," + j + ")"); System.out.print("(" + nextRow + "," + nextCol + ")"); quantity++; } } } System.out.println("\nQuantity of domains is: " + quantity); } ; public static int[][] matrix() throws IOException { File file = new File("src\\array.txt"); int row = 0; int col = 0; int[][] data = new int[6][6]; BufferedReader bufRdr = new BufferedReader(new FileReader(file)); String line; while ((line = bufRdr.readLine()) != null && row < data.length) { StringTokenizer st = new StringTokenizer(line, " "); while (st.hasMoreTokens()) { data[row][col] = Integer.parseInt(st.nextToken()); col++; } col = 0; row++; } ; return data; } };
- 10-25-2012, 04:23 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Task with matrix.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
java matrix liberary
By amrmb09 in forum Advanced JavaReplies: 7Last Post: 12-01-2011, 01:22 PM -
Sparse matrix in java
By Zerpol in forum New To JavaReplies: 2Last Post: 12-15-2010, 12:35 PM -
JAVA task.
By DEA in forum New To JavaReplies: 2Last Post: 04-08-2010, 09:39 PM -
Java Task
By Sokox in forum NetBeansReplies: 4Last Post: 12-13-2009, 09:57 AM -
java-task.com
By marcellis in forum IntroductionsReplies: 0Last Post: 09-28-2009, 10:46 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks