• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populate an array with given values

 
Jody Monahan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I have a really basic question here and it has been driving me up the wall all day .....how do I populate an array with values between 500 and 3000 with increments of 0.05?
I have tried the following:




The current error I get with the above code is that I cannot convert from float to int for the iterator 'i'. I have tried umpteen different variants of the above code but to no avail.
i am sure I am making a small error here and I appreciate any help given.

Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your index starts at 0 and increments with 1. If you could have declarations of different types in a for loop you would want the following:
You will need to move the declaration of either i or f outside the for-loop. Alternatively, you could drop f completely and recalculate its value each time:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
note that you will quickly get rounding errors...
 
Jody Monahan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
note that you will quickly get rounding errors...


Is there anyway to eliminate this round off error?

When I compiled the code, my maximum value went up to 3256.2 rather than 3000....I presume the round-off error is to blame for this?

Cheers.
 
Jody Monahan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was mistakingly using different values there and I do get a value of in and around 3000 (2999.95) to be exact. But my question still stands about eliminating the roundoff error, is it possible?

Thanks again!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may want to read item #20 in our JavaBeginnersFaq, and then follow the links there.
 
Jody Monahan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will do, thanks!
 
Charles Bradley
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, figure out how big the array should be. You were close with (end-start)/step. The answer should be an integer. Hint: suppose the end was 501 instead of 3000. Second, make a short list on a piece of paper, the index and the value to place at the index, like this:
0 500.00
1 500.05
2 500.10
3 500.15
Then figure out the formula for the value when the index is given.
Then write the code, a for loop with the integer index and the end point you calculated, and a body of one assignment with the formula you figured out.
(edited to change 50 to 500.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic