Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Percolation.java (Missing first row and first column)

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is my first post on here so forgive any mistakes in post etiquette.
I'm trying to complete my percolation class using weightedquickfinds and such, however, when I run it I realize I'm missing the first row and first column as seen in picture *see attached* (first is what it's supposed to look like and the second is mine). I assume it is my encode method that is wrong since row=0 and row=1 would have same result. However if i change it so it is always (row - 1) * sizeOfGrid + col, i start getting index out of bound errors since -1*sizeOfGrid is a negative.


Screenshot-2020-09-12-155008.png
[Thumbnail for Screenshot-2020-09-12-155008.png]
 
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a guess but it seems to me that you are not comparing to either the beginning (0) or the end (sizeOfGrid-1) correctly. You might try these changes.

 
Marshal
Posts: 25969
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, welcome to the Ranch!

It looks like the numbers you're working with, which look they come from somewhere outside the program, range from 1 to N. Am I right? I see some code which looks like it maps the range (1 ... N) to range (0 ... N-1), which is what you want to do to deal with Java's annoying zero-origin policy for arrays. But I see flaws in that mapping, for example line 30 shouldn't allow row 0. There may be others; perhaps you're doing the mapping twice in some cases? There's a lot of code and I haven't scanned it carefully.

Perhaps it would be better to have a class which encapsulates the Grid array and manages the mapping between the external 1-origin numbers and the internal 0-origin numbers? Otherwise you're going to find the mapping scattered all over the code, where you might forget to do it occasionally or you might do it twice accidentally.
 
John Luxbrand
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Just a guess but it seems to me that you are not comparing to either the beginning (0) or the end (sizeOfGrid-1) correctly. You might try these changes.


I tried your suggestion, it did not change anything, except that it now always recognizes the grid as percolating. THe actual opening of each space has not changed.
 
John Luxbrand
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Hi John, welcome to the Ranch!

It looks like the numbers you're working with, which look they come from somewhere outside the program, range from 1 to N. Am I right? I see some code which looks like it maps the range (1 ... N) to range (0 ... N-1), which is what you want to do to deal with Java's annoying zero-origin policy for arrays. But I see flaws in that mapping, for example line 30 shouldn't allow row 0. There may be others; perhaps you're doing the mapping twice in some cases? There's a lot of code and I haven't scanned it carefully.

Perhaps it would be better to have a class which encapsulates the Grid array and manages the mapping between the external 1-origin numbers and the internal 0-origin numbers? Otherwise you're going to find the mapping scattered all over the code, where you might forget to do it occasionally or you might do it twice accidentally.



Thank you for the welcome! Indeed the numbers are coming from a text file that companies it. The one i used for the picture is attached below(oh i cant attach text files). Unfortunately, due to my assignment, I'm not to create any extra classes and have to adhear to the methods and classes already given. Below is the weightedquickunion class that I am to use (i'm not to change it at all)

 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is suspect.
IF row is 0 then row * sizeOfGrid is also zero so the result is just col. Same is true if row is 1.
 
John Luxbrand
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, i tried just row * sizeOfGrid + col but it still does not seem like it changes anything
 
Paul Clapham
Marshal
Posts: 25969
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Luxbrand wrote:Unfortunately, due to my assignment, I'm not to create any extra classes and have to adhear to the methods and classes already given.



Well, that's a pity. (I hate assignments which require you to do things in a bad way.) But it's also a pity because we can't tell which of the methods there can be changed and which of them can't.
 
Paul Clapham
Marshal
Posts: 25969
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Luxbrand wrote:yeah, i tried just row * sizeOfGrid + col but it still does not seem like it changes anything



It should work if "row" and "col" are 0-origin numbers (i.e. between 0 and N-1). If they aren't, well, maybe it will still work, but I don't really know what it's supposed to do -- it looks like maybe it's supposed to convert an array entry's row and column index to a single distinct number, so knowing what origin is relevant here is critical.
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Luxbrand wrote:yeah, i tried just row * sizeOfGrid + col but it still does not seem like it changes anything

The 'row' argument to this method, is it one based or zero based?
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be helpful to rename your 'row' variables to 'row1' and 'row0' depending on whether they are one based or zero based. As it is now we're just left to guess.
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF ZERO base this should be:
IF ONE based this should be:
You have to KNOW what you are dealing with and not just be guessing.
 
Bartender
Posts: 4109
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice: make everything zero-based, so no messing around with row1 and row1. I would read the input and first put it in a List. Then check for the minimum value. Is that say 2, withdraw 2 from all the numbers in the list and then build the quickfind array.

Then check for the consistenr use of this zaro-based stuff. If you need help with that, then please supply the gui as well.
 
It runs on an internal combustion engine. This ad does not:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic