wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Populate an array with given values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Populate an array with given values" Watch "Populate an array with given values" New topic
Author

Populate an array with given values

Jody Monahan
Greenhorn

Joined: Feb 10, 2010
Posts: 20
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

note that you will quickly get rounding errors...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jody Monahan
Greenhorn

Joined: Feb 10, 2010
Posts: 20
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

Joined: Feb 10, 2010
Posts: 20
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

Joined: Oct 02, 2003
Posts: 11160
    
  16

you may want to read item #20 in our JavaBeginnersFaq, and then follow the links there.
Jody Monahan
Greenhorn

Joined: Feb 10, 2010
Posts: 20
Will do, thanks!
Charles Bradley
Greenhorn

Joined: Oct 04, 2009
Posts: 25
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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populate an array with given values
 
Similar Threads
valid assignment
Array two dimension
Triangular Wave
double (float) accuracy