Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

display error

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am suppose to display my memory state after storing a value. I wrote a test class that stores a value 5 in index 16, but when I call the toString method it displays it in index 15. Can anyone help?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because getValue() sets its addr argument to 0 before using it, it will only ever return the value in mem[0]. I'm not sure what that assignment is for!

As for what "toString()" will display: my head hurts just trying to follow it! Partly this is due to the use of all the "magic numbers" like 17, 9, 8, and 16. Without knowing what these represent, it's impossible to picture what the intent is here.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well like I said it gets stored correctly. But it does not get displyed properly. Here is what it should look like.

Bt the 5 gets put in the 15 place. Each number represents a place in the array.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this display can't come from the code snippet above; the getValue() method, as shown, will always return the same value (mem[0]).
So I don't think we're looking at the code you're actually running.

But now that I see what toString() is supposed to do here, I can see several problems with it. For example, it looks like each row is supposed to show 16 contigous elements with a tab after the first 8. But you're actually skipping an element in the middle of each row; the upper limit of the left half is "< j+8", while the lower limit of the right if "= j+9". The element at index j+8 is skipped. Similarly, although you're printing 16 items in each row, you're incrementing j by 17 each time, so that if you fix the above, the item at the end of each row will be skipped.

In any case, if you fix both of these, you'll see that element at the beginning of the second row wrap back around to be at the end of the first row, which I gather is what you want.
[ September 25, 2006: Message edited by: Ernest Friedman-Hill ]
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fisrt of all here is the new code

test stuff still the same. Now when I run it I see a 5 at the end of the first line and a 5 at the beginning of the second line.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each row has one more element in it than you intend; one element is replicated at the end of the each row and the beginning of the next.

You want to increment j by 16 for each row, which is what you're doing now. It should be 16 since that's how many items you're displaying in a row.

For each row, the first group is indices j+0 through j+7 (i.e., the loop condition is i < j+8), and the second group is indices j+8 through j+15 (i.e., i < j+16). This isn't quite what you're doing; if you adjust your indices accordingly, things should work OK.

I bet graph paper would have helped you work this out. If you marked off a square area of the grid, 16 cells wide, and marked the leftmost element in each row with its index, and then the eighth and ninth with theirs, and the rightmost as well, you would have been able to figure this out.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help. I did finally figure it out. my numbers where just messed up. thanks for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic