Skip to content

Assignment4 - answer#5

Open
alice006 wants to merge 21 commits intomasterfrom
Dev_branch
Open

Assignment4 - answer#5
alice006 wants to merge 21 commits intomasterfrom
Dev_branch

Conversation

@alice006
Copy link
Owner

I created the Assignment4 folder and I tried to clean this repo in order to have all the assignments organised.

@alice006 alice006 requested a review from simon-frankau June 26, 2017 22:44
Copy link
Collaborator

@simon-frankau simon-frankau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Algorithm looks basically ok, but there's a bug, and as there are no tests it doesn't seem to have been caught.

{
int CountingIslands(int noRows, int noCols, boolean[][] landOrWater)
{
int index, indexj;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got a mixture of tabs and spaces, and it looks like your editor has tab == 4 spaces. When viewed on github, which thinks tab == 8 spaces, it looks wonky.

The best thing to do is be consistent, and always use just tabs or spaces. The Google coding style uses only spaces. You should be able to set your text editor up to always use just one or the other.

// Count the islands
int count = 0;

for (index = 0; index < noRows; index++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use 'row' or 'r' or something that makes the variable tie back to it being a row-counter. "index" is not very helpful. You could just use "i", as it's about as informative.

Short names are fine if they're obvious. Long, unhelpful names are never good. :)


for (index = 0; index < noRows; index++)
for (indexj = 0; indexj < noCols; indexj++)
if (landOrWater[index][indexj] == true && !visited[index][indexj])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"X == true" is just "X".

if (landOrWater[index][indexj] == true && !visited[index][indexj])
{
DFS(index, indexj,landOrWater, visited);
count = count++;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bad in Java, absolutely horrible in C/C++, where it's undefined behaviour. You're basically saying "Get me the value of 'count', and increment it later, and take that value and assign it to 'count'." In Java, the post-increment happens before the assignment, so 'count' gets re-assigned back to its old value. In other languages, it's left undefined.

Any of the following would be ok:
count++;
++count;
count += 1;
count = count + 1;

// DFS for all neighbours
for (int index = 0; index < 4; index++)
if (isSafe(indexRow + rowNeighb[index], indexCol + colNeighb[index], landOrWater, visited) )
DFS(indexRow + rowNeighb[index], indexCol + colNeighb[index],landOrWater, visited);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move the isSafe check to the top of DFS, and then expand this loop into just 4 recursive DFS calls, which are clearer than this loop and sets of arrays. Sometimes data-driven structures are better, but I don't think this case is complex enough to make it worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants