• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays, IndexOutOfBounds,Error by one, Accessing element - noob LF advice.  RSS feed

 
S. Freeman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment:
It is a well-researched fact that men in a restroom generally prefer to maximize
their distance from already occupied stalls, by occupying the middle of the longest
sequence of unoccupied places.
For example, consider the situation where ten stalls are empty.
_ _ _ _ _ _ _ _ _ _
The first visitor will occupy a middle position:
_ _ _ _ _ X _ _ _ _
The next visitor will be in the middle of the empty area at the left.
_ _ X _ _ X _ _ _ _
Write a program that reads the number of stalls and then prints out diagrams in the
format given above when the stalls become filled, one at a time. Hint: Use an array of
boolean values to indicate whether a stall is occupied.



To best explain what i want to ask advice on i will start by explaining my though process. I spent a lot of time trying to come up with the solution on paper(i do this first before actually trying to code since i noticed that it helps me think more than just mindlessly tweaking and trying to code on the run so to say). And for a LONG time i could not come up with an idea how to access last element of the array(since if element with index 9 is the last free spot (8 + 9) / 2 = 8.5 so basically an 8 because i am working with integers. Finally i gave up and just wanted to solve it already.
And the second issue i had is index out of bounds error reported for line 32. while(i < arr.length && !arr[i] ), that used to be while(!arr[i] && i < arr.length). I thought that it was an error by one and that i should just adjust the i < arr.length to i < arr.length - 1, but then i still would not be able to access the last element in the array(in my case the one with index 9).
So the next thought that came to mind is that the && does not check the second part of condition because i am trying to access an element that does not exist(instant error report, second part of the condition not even checked). So i decided to switch places of the two conditions because if i > arr.length the second part of the condition wont be checked because the first part is already false. But the value of i still reaches arr.length ( in my case 10). Which did allow me access the last element in the array.

But i do not feel like i came up with the solution, more that it somehow worked out because of tweaking, modifications. Any advice on how i should have approached solving this, or did i fail to grasp something about arrays that did not allow me to come up with the solution on the conceptual level ( did i used the right word there, basically i mean on paper).
I hope that my code is neat enough and sorry if i did not explain the issue that is bothering me good enough, English is not my native language.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paper is a good first step so I'm glad you started there. Another thing that will happen over time is that you'll recognize idioms (common coding constructs) and idioms.

For example, the while condition is doing two things here which is harder.



A common idiom is to have a for loop with an if statement. The for loop is only responsible for incrementing the array index and seeing if it is done.


Before the loop, you initialize "temp2" to what it should be if you don't find an answer. Then in the if statement (inside the loop), you set temp2 to where you find a match. Or the last match in your case.
 
Henri Melissimo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing on paper is definitely a very first good step.
Another thing you can think about is how to use OO principles to make your code reusable and cleaner.
For example, think about a way to write a class, that encapsulates a given empty spaces in the restroom.


Then think about writing some code, that scans arrays of any size and constructs instances of available spaces and stores them in a collection (maybe sorted by size?).
You can then manipulate those EmptySpace class instances at will.
A good think I recommend young programmers to do, is to always write small unit tests for their classes. And as they progress through their program, they can quickly test their code.
I know this might be a little out of the scope of the question you asked, but I think the earlier you learn good habits, the better of a programmer you will become.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!