aspose file tools*
The moose likes Beginning Java and the fly likes Calculating the average of an unknown amount of numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculating the average of an unknown amount of numbers" Watch "Calculating the average of an unknown amount of numbers" New topic
Author

Calculating the average of an unknown amount of numbers

Darrin Altman
Greenhorn

Joined: Jul 10, 2011
Posts: 15

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;
}
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

sumOfNumbers / numberOfNumbers = average

Good luck.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Darrin Altman
Greenhorn

Joined: Jul 10, 2011
Posts: 15

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?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
average += score / ?;

Is this a valid average calculation? Calculate the average after the loop.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Darrin Altman
Greenhorn

Joined: Jul 10, 2011
Posts: 15

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61214
    
  66

Can you figure out how to add 1?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
apchar boiir
Greenhorn

Joined: Sep 07, 2004
Posts: 18
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61214
    
  66

double?
apchar boiir
Greenhorn

Joined: Sep 07, 2004
Posts: 18
yeah yeah, counters are usually integers (int)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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

Joined: Jul 10, 2011
Posts: 15

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
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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

Joined: Jul 10, 2011
Posts: 15

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

Joined: Jul 10, 2011
Posts: 15

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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculating the average of an unknown amount of numbers