This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to write an algorithm for an average of values?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to write a method that uses a sentinel loop that repeatedly asks a user for input until a negative number is entered, and then prints the average.
I wrote the following code so far, but I can't figure out how to write an algorithm for the average? Any help is appreciated, thanks!

 
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the average is just the sum of the values divided by the number of values, isn't it?
 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:But the average is just the sum of the values divided by the number of values, isn't it?



Yes, but I don't get how to represent the number of values. Is the for loop approach correct? Thanks!
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a couple things I would do with this method. You have some redundant code going on which can be eliminated by enclosing your prompt in a loop. This can be done several ways, one of which would be to declare a new variable and use it as your check to see if the loop continues also I would probably use an if/else construct not a while loop but that may just be preference. I am new here so I am not going to post code as I do not think it is allowed but to try to answer your original question you simply need to store your inputs in an ArrayList. Once your sentinel is entered call all of the elements in your ArrayList and do the average calculation. You will want to use an arraylist because it is a dynamic collection, meaning it grows in size as you need it to. So you will have an algorithm that calls all of your arraylist elements, adds them together and then divides that sum by the size of the arraylist. This is a very abridged version of what needs done, if you have never worked with collections let me know and I will do my best to explain it in more detail. If you need help with the actual code I will do all I can without violating the rules of this community.
 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for the detailed answer, but I'm in introductory Java in high school and we haven't learned array lists yet. Right now we're working with while loops, so I figured I should probably use those. I'm just having trouble figuring out how to write an algorithm to find the average. How can I set a value for the number of values entered by the user?
Thanks!
 
John C Stewart
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you mean to track how many responses have been entered before the sentinel and subsequent termination you would use a counter variable and increment it with each pass of the loop
 
Marshal
Posts: 79969
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not testing for negative entry. You need to look at the while loop very carefully.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic