Write a recursive method that counts the number of creatures on a grid. An creature is defined as a contiguous collection of 1's (connected horizontally and vertically).
Input
Max dimensions of the array
Distribution of organisms on the array
Output
The number of creatures and the layout of the grid showing the locations of the creatures.
Requirements checklist:
q Documented source code (include any assumptions you make)
Input File (in.txt)
The format of the input file is as follows: First line is the dimensions of the grid.. Then the grid containing 1s for the creatures and 0s for the spaces. The file terminates by 0 0.
5 5
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
5 5
0 0 0 0 1
0 1 1 1 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0
Output File (out.txt)
Experiment # 1
Number of Creatures: 1
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Experiment #2
Number of Creatures: 3
0 0 0 0 1
0 2 2 2 0
3 0 0 0 0
0 0 0 0 0
0 0 0 0 0


VulpesRank #55 · 29.2K reputationPosted Oct 26, 2013, 1:47 PM EDT
VulpesRank #55 · 29.2K reputationPosted Oct 27, 2013, 8:08 AM EDT
y = Integer.parseInt(split[0]);
should, of course, be:
y = Integer.parseInt(split[1]);
I've corrected it above though it made no difference to the results as x and y were the same :)
SUNIL GUTTARank #1,831 · 810 reputationPosted Oct 28, 2013, 4:06 AM EDT
Well tried to check the code .. i can say for sure you given good code .. but there are lapses in me to understand things ,, so please help me further sorry for re-insisting
there is compiling error don't know how ..
how many classes in ur code ?
and how can I test it ? please explain in brief details details .. thanks
VulpesRank #55 · 29.2K reputationPosted Oct 28, 2013, 6:23 AM EDT
But, if you are using the online compiler, you'll need to remove the 'public' from Creatures as we discussed in another thread.
You also need the input file, in.txt, which on my machine is in the same folder as the executable. Hopefully, you''l be able to upload this file or type it online with ideone.
However, I've just noticed that there's a missing 'import' in the code I posted (must have been a copy and paste error on my part). The imports should be :
import java.util.*;
import java.io.*;
SUNIL GUTTARank #1,831 · 810 reputationPosted Nov 3, 2013, 5:23 PM EST
well sometimes i hate these un-usual exceptions .. . . everything is fine .. build successful .
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Creatures.main(Creatures.java:53) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) // this is ar Creatures class
Any for loop above is doing infinate itirations or array limit overflowed ? .. i don't think so .. code looks perfect ?
any ideas for this exception ..
VulpesRank #55 · 29.2K reputationPosted Nov 4, 2013, 1:26 AM EST
To do anything like this you need to know exactly how the text file is laid out.
The code assumes that there will be a blank line between each pair of lines in the file as this is what there appeared to be in your opening post. If there isn't a blank line or there's more than one, then the code won't work properly.
So I'd check the file. If in fact there are no blank lines, then it's easy to adjust the code for that: just remove the 2 lines which are commented - skip empty line.