Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Populate an array with given values

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
note that you will quickly get rounding errors...
 
Jody Monahan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will do, thanks!
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic