Results 1 to 8 of 8

Thread: Coding help

  1. #1
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Java_Fanatic View Post
    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.

  6. #6
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes that logic is totally wrong. Look at the following code segment.

    Java 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.

Similar Threads

  1. Better coding
    By tomiu in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 08:19 PM
  2. Socket coding
    By Neptunes07 in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 09:53 PM
  3. Help with really simple coding
    By tigertomas in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 05:47 AM
  4. coding help
    By accies76 in forum New To Java
    Replies: 5
    Last Post: 11-12-2008, 09:15 PM
  5. Help with program coding
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •