|
Ok, so this project is due in 2 hours, and I've been working on it all day. It's pretty much done but it's just not working and I just can't stare at it any more. Can someone help?
The project is basically analyzing contiguous spaces in a two-dimensional array (find the blobs). A blob is 2 or more contiguous spaces in the array. I've figured out how to find the blobs using a stack, and I think I implemented it correctly but it's not working for some reason. The code hangs within the while loop so I'm assuming something in there is wrong. here is the code:
http://pastebin.com/d30e116f3
^^pastebin now.
I know its way over commented, I'm a total noob at c++ (mostly code in java,asm, and python) and it got confusing with braces and shit. I also know this could be done way more efficiently but I don't really care at this point, I just need it to work. The following DOES work: importing grid from text file, displaying the grid, and everything else. the above method hangs in the while loop and I can't figure out why. Help PLEASE!
|
|
with no identation its impossible to read anything
|
|
Wow that's way to messy, sorry I'm not touching that was the //startif //endif comments really necessary? lol
Where exactly does it hang? use the debugger, might give you a clue
|
On October 14 2009 11:08 Eti307 wrote: Wow that's way to messy, sorry I'm not touching that was the //startif //endif comments really necessary? lol
Where exactly does it hang? use the debugger, might give you a clue
the startif endif i put it so i could check and see if i was missing a bracket when i was fixing syntax errors. i can clean it real fast.
|
What are the INPUTs and OUTPUTs of this function supposed to be?
|
On October 14 2009 11:12 tossinYoSalad wrote:Show nested quote +On October 14 2009 11:08 Eti307 wrote: Wow that's way to messy, sorry I'm not touching that was the //startif //endif comments really necessary? lol
Where exactly does it hang? use the debugger, might give you a clue the startif endif i put it so i could check and see if i was missing a bracket when i was fixing syntax errors. i can clean it real fast.
instead of doing that just type in both brackets each and everytime you are using something with brackets. Prevents those type of errors
|
On October 14 2009 11:12 yenta wrote: What are the INPUTs and OUTPUTs of this function supposed to be?
all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function.
there is no input it acts on a grid held in the class (just a two-dim array of ints) its called gridCopy. theGrid is the same thing only its an arr of chars (this teacher is psycho, i had to do it this way) i know its a horrible implementation believe me.
|
On October 14 2009 11:16 Eti307 wrote:Show nested quote +On October 14 2009 11:12 tossinYoSalad wrote:On October 14 2009 11:08 Eti307 wrote: Wow that's way to messy, sorry I'm not touching that was the //startif //endif comments really necessary? lol
Where exactly does it hang? use the debugger, might give you a clue the startif endif i put it so i could check and see if i was missing a bracket when i was fixing syntax errors. i can clean it real fast. instead of doing that just type in both brackets each and everytime you are using something with brackets. Prevents those type of errors
ya i do, i also needed to put a a few lines in specific places at the end and there were 5 braces in a row at the end lol.
|
On October 14 2009 11:16 tossinYoSalad wrote:Show nested quote +On October 14 2009 11:12 yenta wrote: What are the INPUTs and OUTPUTs of this function supposed to be? all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function. i know its a horrible implementation believe me.
Rewrite it like this:
RECURSIVE CALLER Initialize count Initialize array of array with flags (w/e you want) FOR( 0...x ) FOR( 0..y ) if point @ x,y is unchecked & x.y is a space, call recursive worker //FOR //FOR return w/e
RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count ) base case: return if out of bounds return if space you're looking has been marked 'looked at' recursive case: count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count. if adj spaces > 0 & root == true, increment counter //endif mark current space as read END
|
On October 14 2009 11:29 yenta wrote:Show nested quote +On October 14 2009 11:16 tossinYoSalad wrote:On October 14 2009 11:12 yenta wrote: What are the INPUTs and OUTPUTs of this function supposed to be? all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function. i know its a horrible implementation believe me. Rewrite it like this: RECURSIVE CALLER Initialize count Initialize array of array with flags (w/e you want) FOR( 0...x ) FOR( 0..y ) if point @ x,y is unchecked & x.y is a space, call recursive worker //FOR //FOR return w/e RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count ) base case: return if out of bounds return if space you're looking has been marked 'looked at' recursive case: count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count. if adj spaces > 0 & root == true, increment counter //endif mark current space as read END
thanks alot, what do you mean by root though? and because the 2d array is a class var i dont need to pass it though do i?
|
I only very briefly skimmed it so I may be way off, but it looks like your bounds checking is wrong?
Your Code: if (rowPtr-1 > 0) if(rowPtr+1 < theGrid.getRows())
I'm assuming you're trying to make sure that 0 <= rowPtr < theGrid.getRows(). If that's the case, then shouldn't it be:
if (rowPtr+1 > 0) if(rowPtr < theGrid.getRows())
|
On October 14 2009 11:38 Slithe wrote: I only very briefly skimmed it so I may be way off, but it looks like your bounds checking is wrong?
Your Code: if (rowPtr-1 > 0) if(rowPtr+1 < theGrid.getRows())
I'm assuming you're trying to make sure that 0 <= rowPtr < theGrid.getRows(). If that's the case, then shouldn't it be:
if (rowPtr+1 > 0) if(rowPtr < theGrid.getRows())
uhh you were sorta right, but you DID point out an error it needs to be if(rowPtr-1 >= 0) otherwise itll never enter those code segments on the leftmost or rightmost bounds.
but yes it needs to be within the bounds of [0,theGrid.getRows()], but because it can only exceed the left bound on a -1, and it can only exceed the right bound on a +1 i ommited those calls.
fixed it to that and still didnt work. I think its a stack error in the original code, but now im rewriting with yenta's suggestion, only thing i dont get is how to actually count the blobs using that method, i can get it to pass over all the blocks, but no way to tell when the end of a blob has been reached.
|
due in 2 hours.. if it was due in 2 days, i could help
|
|
Well, that's a major change from when I started looking at it...
As far as I can tell, the previous version of the code did not work because you never modified the value of a cell when you added it to the stack in the while loop. And since you keep on adding more items to the stack which each added more since nothing was being ruled out on subsequent loops, the while loop never ends. Also, you are probably supposed to check your bounds to be "var >= 0" instead of "var > 0" unless you don't need to check the 0 index for some reason.
In your new code your upper bound should be comparing ">=" theGrid.getRows() instead. Same for the .getCols() one.
else if(gridCopy[curr_x][curr_y] == 0){return;} Use double equal signs for comparisons. Common mistake.
gridCopy[curr_x][curr_y] = 0; That should go before you make the recursive calls or you'll get the same error.
Hope this helps.
|
No idea what this thing is supposed to do, but your counter doesn't increment.
edit: oh, didn't read the whole OP I guess
|
I'm pretty sure that's supposed to count the number of blobs. Unfortunately, the code doesn't ever determine whether each location is part of a blob or not.
|
On October 14 2009 11:34 tossinYoSalad wrote:Show nested quote +On October 14 2009 11:29 yenta wrote:On October 14 2009 11:16 tossinYoSalad wrote:On October 14 2009 11:12 yenta wrote: What are the INPUTs and OUTPUTs of this function supposed to be? all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function. i know its a horrible implementation believe me. Rewrite it like this: RECURSIVE CALLER Initialize count Initialize array of array with flags (w/e you want) FOR( 0...x ) FOR( 0..y ) if point @ x,y is unchecked & x.y is a space, call recursive worker //FOR //FOR return w/e RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count ) base case: return if out of bounds return if space you're looking has been marked 'looked at' recursive case: count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count. if adj spaces > 0 & root == true, increment counter //endif mark current space as read END thanks alot, what do you mean by root though? and because the 2d array is a class var i dont need to pass it though do i? look at this: http://pastebin.com/m7c4ab47d
|
|
|
|