• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculating the average of an unknown amount of numbers

 
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working on a programming question from "The Art and Science of Java". I need to allow the user to input any amount of numbers they want and then when they finish produce the average of their numbers. I wrote a program where I add all of the numbers input but then I'm stuck on the division part because I don't know how many different numbers they input? Any help appreciated. Here is what I have so far:

public void run() {
double average = 0.0;
while (true) {
double score = readDouble("Enter grade: ");
if (score == SENTINEL) break;
average += score / ?;

}

println("The average is: " + average);
}
private static final int SENTINEL = -1;
}
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sumOfNumbers / numberOfNumbers = average

Good luck.
 
Darrin Altman
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:sumOfNumbers / numberOfNumbers = average

Good luck.



Since the user can input any amount of numbers how can I know the numberOfNumbers or have my program calculate that? If I knew in advance how many they would input then I would, but if not?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

average += score / ?;


Is this a valid average calculation? Calculate the average after the loop.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the first thing you should always do is work out how you'd do something by hand - i.e. using paper and pencil.

so, assume I was going to read a list of numbers, and I want you to tell me the average of them once I tell you i'm done. How would you do that?
 
Darrin Altman
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:the first thing you should always do is work out how you'd do something by hand - i.e. using paper and pencil.

so, assume I was going to read a list of numbers, and I want you to tell me the average of them once I tell you i'm done. How would you do that?



I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you figure out how to add 1?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need another variable: a counter that keeps track of how many numbers have been entered. Remember that any variable can be added to itself. For instance:

double x = 4;
x = x + 1;

after these lines are executed, x will be 5
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
double?
 
apchar boiir
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah yeah, counters are usually integers (int)
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darrin Altman wrote:I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.



So how do you count how many numbers are added? each time you hear a new value from me, you increase your count by 1.

So you need to keep track of two things...the running total of all the input numbers, and a count of how many numbers you've gotten.
 
Darrin Altman
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Darrin Altman wrote:I would count how many numbers you read off and then divide by that number. Contemplating this I am trying to think of a method to figure out how many numbers the user input but am at a loss. I am going to go back and read the chapter again and see if I can figure this out.



So how do you count how many numbers are added? each time you hear a new value from me, you increase your count by 1.

So you need to keep track of two things...the running total of all the input numbers, and a count of how many numbers you've gotten.



I really don't believe up to this point the book has shown how to set up an input counter. I will continue to work on this and report back.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is nothing fancy about it...it would just be a variable you create. just like you have your "double average = 0", you would set up a "int numInputs = 0".


Then each time you get a new value input, you add 1 to it. there are several ways of doing that:

numInputs = numInputs + 1;

or

numInputs += 1;

in fact, adding 1 to an int is so common, there is a special shortcut:

numInputs++;


 
Darrin Altman
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:there is nothing fancy about it...it would just be a variable you create. just like you have your "double average = 0", you would set up a "int numInputs = 0".


Then each time you get a new value input, you add 1 to it. there are several ways of doing that:

numInputs = numInputs + 1;

or

numInputs += 1;

in fact, adding 1 to an int is so common, there is a special shortcut:

numInputs++;




As soon as I get home from work I will work with this and report back. Thank you very much for your patience and guidance!
 
Darrin Altman
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completed the exercise. Thank you to everyone who gave input, especially Fred. I added a couple of lines at the end just to verify the program was running correctly. I realized I had to put the counter after break because if not it would count the input of the sentinel. I'm sure this could be refined and improved but it works. Thank you all again.

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use tabs for indenting. Use spaces.

I don't like the break. Try thisYes, you do need the second pair of () around the = operator in the line beginning while, otherwise you get the operator precedence wrong.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell's post illustrates the difference in coding style. Personally, I find this line too complicated:

while((score = readInt("Enter next score, or " + SENTINEL + " to exit: ")) != SENTINEL)

IMHO, there is too much going on there - you're building a string, getting user input, saving it comparing it to another value and controlling a while loop.

Whatever happened to "one line of code should do one thing"?

It's certainly not WRONG - it's just not a line I would ever write.

just something to think about.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darrin Altman wrote:



Again, there is nothing wrong with this. but personally, I would not calculate the average on each iteration. there's no need for that. I'd move that line outside the loop, and only calculate it once.

It won't make much difference here, but if you were doing some INTENSIVE computation that took a long time, why waste the CPU cycles? it does nothing but slow down your program. Further, at some point, someone else will come along and read your code (possibly yourself) and wonder why you keep re-calculating the average on each loop.

Just more to ponder.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic