Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

For loop with parameter N that determines number of terms

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there! I am stuck on a study guide problem for class...

a. Write a code that is written inside a body of a method named average that takes two parameters: N that determines number of terms you should calculate the average of and lowBound that is the beginning term of the geometric sequence. if lowBound is 4 and N is 3, then the average of 4, 8, 16 is calculated and returned.

My code runs fine if I set the test as 16, but I can't figure out what I could do to N to have it determine the number of terms. This is what I have so far...



Any help would be appreciated.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To calculate that value you call N , you should use this to calculate it =


double x=lowBound * Math.pow(2,(N-1));
for (double i=lowBound; i<=x;i*=2)

the x is the limiting value of your loop..e.g, if you give lowbound 4 & N is 4, your sequence is 4,8,16,32. x in this case will be 4 * (2,3) = 4*(2*2*2) =32. Works? Let me know.
 
Trish Huynh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ak Jo wrote:To calculate that value you call N , you should use this to calculate it =


double x=lowBound * Math.pow(2,(N-1));
for (double i=lowBound; i<=x;i*=2)

the x is the limiting value of your loop..e.g, if you give lowbound 4 & N is 4, your sequence is 4,8,16,32. x in this case will be 4 * (2,3) = 4*(2*2*2) =32. Works? Let me know.



I never thought about it that way. Thanks so much!
 
Marshal
Posts: 70353
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Always use them: doesn't it look a lot better You cannot use coloured text inside code tags, however.

Never use floating point numbers in a loop variable. There is a risk that you get 9.99999999999999something instead of 10 (or similar) and your loop will not work correctly. You ought to work that out using integer numbers. Remembering that the following integer divisions all equate to 1
3 / 2
2 / 2
5 / 4
1 / 1
and 1 / 2 is 0, consider using the loop with /= 2 instead. Start big and use i > 0 or i > 1 or similar as your continuation condition.
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic