Results 1 to 6 of 6
Thread: Java Task with matrix.
 10162012, 06:15 PM #1Member
 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)}.
 10172012, 12:20 AM #2Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 7
Re: Java Task with matrix.
Hi, every one! Please help me with my task. I don't know which way to think
 10242012, 01:39 PM #3Member
 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; 10252012 at 12:32 PM.
 10252012, 09:12 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,680
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
 10252012, 04:33 PM #5Member
 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; } };
 10252012, 05:23 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,680
 Blog Entries
 7
 Rep Power
 21
Similar Threads

java matrix liberary
By amrmb09 in forum Advanced JavaReplies: 7Last Post: 12012011, 02:22 PM 
Sparse matrix in java
By Zerpol in forum New To JavaReplies: 2Last Post: 12152010, 01:35 PM 
JAVA task.
By DEA in forum New To JavaReplies: 2Last Post: 04082010, 10:39 PM 
Java Task
By Sokox in forum NetBeansReplies: 4Last Post: 12132009, 10:57 AM 
javatask.com
By marcellis in forum IntroductionsReplies: 0Last Post: 09282009, 11:46 AM
Bookmarks