# Coding help

• 10-14-2009, 04:31 AM
Java_Fanatic
Coding help
Got a quick question if anyone can help. I got an assignment for this problem:

Give a Java loop statement that will read in a list of numbers of type double and then output their average. The numbers are all greater than or equal to 1.0. The list is ended with a sentinel value. Please specify the sentinel value and give any declarations or initializing statements that are needed.

Here's the code I have for this so far:

import java.util.Scanner;
public class number1
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double num, avg, next, sum, count;
avg = 0;
sum = 0;
count = 0;
System.out.println("Enter numbers.");
num = scan.nextDouble();
count++;
next = scan.nextDouble();
while(next > -1.0)
{
next = scan.nextDouble();
count++;
sum = num + next;
avg = sum / count;
System.out.println(avg);
}
}
}

The sentinel value is -1. I have the averaging working, except when i try to average more than 2 numbers, I get the wrong average printed out. Can someone help me figure this out?
• 10-14-2009, 04:45 AM
Fubarable
One problem: You are including the -1.0 value in the sum.
• 10-14-2009, 04:48 AM
Java_Fanatic
i don't get it. Are you talking about it in the while loop? Should i change that, and if so, to what?
• 10-14-2009, 05:04 AM
Fubarable
Best for you to see it for yourself. I recommend that you step through your code manually with a very small list of numbers, say 3, and see what happens. Then you'll see your error.
• 10-14-2009, 01:58 PM
Eranga
Quote:

Originally Posted by Java_Fanatic
i don't get it. Are you talking about it in the while loop? Should i change that, and if so, to what?

First of all think the mathematical way/logic to find the average. As Fubarable says think what happen if you are using three numbers and what happen with your code.
• 10-14-2009, 02:18 PM
Java_Fanatic
i step-traced through my code with the numbers 4, 5, and 6. I kinda see my error, but I still don't know how to fix it. What my method does with these 3 numbers is:

num gets assigned the value 4.
count goes to 1.
next is assigned a 5.
next becomes 6.
sum adds 4 and 6 and gives you 10.
avg divides 10 by count, which is 2, and prints out 5 for the avg. If you add 4, 5, and 6, and divide by 3, you do get 5, but then i get another println with another avg.

I get most of it, I just don't see the error. Do I need to add anything?
• 10-14-2009, 03:28 PM
Fubarable
You've got sum which is obviously there to hold your numeric sum and then two other vars, num and next both seemingly for the same purpose. Would things be simpler if you had only one var here instead of both num and next? Also, for a sum variable to work, you must add the newly obtained number to the sum a la

sum = sum + newlyObtainedNumber.

Are you doing this in your code?

Then think on what happens when the sentinal value of -1 is reached. When this happens, should the program add this -1 number into the sum and increase the counter, or should it try to skip over those steps?

The key to solving this and similar problems is to first write the steps down on paper and then translate that into Java. Keep at it, you'll get there. Also, after thinking through it one more time, why not post your most recent best attempt? If you do, please use code tags.

Much luck!
• 10-15-2009, 04:37 AM
Eranga
Yes that logic is totally wrong. Look at the following code segment.

Code:

```        next = scan.nextDouble();         while (next > -1.0) {             next = scan.nextDouble();             count++;             sum = num + next;```
In the first line you get the user input and assign the value to next variable. Say that next is greater than -1.0, jumps into the while loop. First line in the while loop you get the value from user again and assign it into the same variable, next so the initial value is overwrite basically. Say you enter -1 in that case, because earlier you've entered two numbers and expecting the average of that. But it's wrong.