• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there an easier way to write this?  RSS feed

 
niraj pateI
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void Calculate()
{
frequency[0] = middleC;
frequency[1] = middleC * (Math.pow(2,(0.0833)));
frequency[2] = frequency[1] * (Math.pow(2,(0.0833)));
frequency[3] = frequency[2] * (Math.pow(2,(0.0833)));
frequency[4] = frequency[3] * (Math.pow(2,(0.0833)));
frequency[5] = frequency[4] * (Math.pow(2,(0.0833)));
frequency[6] = frequency[5] * (Math.pow(2,(0.0833)));
frequency[7] = frequency[6] * (Math.pow(2,(0.0833)));
frequency[8] = frequency[7] * (Math.pow(2,(0.0833)));
frequency[9] = frequency[8] * (Math.pow(2,(0.0833)));
frequency[10] = frequency[9] * (Math.pow(2,(0.0833)));
frequency[11] = frequency[10] * (Math.pow(2,(0.0833)));
frequency[12] = frequency[11] * (Math.pow(2,(0.0833)));
frequency[13] = frequency[12] * (Math.pow(2,(0.0833)));

}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to write loops yet?
 
niraj pateI
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Do you know how to write loops yet?


yes
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was really a hint, more than an actual question.

Any time you see a hunk of code doing the same thing over and over and over, with the only difference being the index of an array, you should think "How can I write this using a loop of some kind".

Note that this line:

could have been written like this:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't 0.0833. Use 1.0 / 12.0. Don't call pow twelve times. Call it once and get a double value for the twelfth root of 2.
I thought 0.0833 would be dreadfully imprecise, but using that twelve times comes to 1.9995... I was mistaken.
Where do you get frequency[13] from?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!