Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2009, 05:31 AM
Member
 
Join Date: Oct 2009
Posts: 12
Rep Power: 0
Java_Fanatic is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-14-2009, 05:45 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,391
Rep Power: 8
Fubarable is on a distinguished road
Default
One problem: You are including the -1.0 value in the sum.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-14-2009, 05:48 AM
Member
 
Join Date: Oct 2009
Posts: 12
Rep Power: 0
Java_Fanatic is on a distinguished road
Default
i don't get it. Are you talking about it in the while loop? Should i change that, and if so, to what?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-14-2009, 06:04 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,391
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-14-2009, 02:58 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,454
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-14-2009, 03:18 PM
Member
 
Join Date: Oct 2009
Posts: 12
Rep Power: 0
Java_Fanatic is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-14-2009, 04:28 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,391
Rep Power: 8
Fubarable is on a distinguished road
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!
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-15-2009, 05:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,454
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Better coding tomiu New To Java 1 04-09-2009 08:19 PM
Socket coding Neptunes07 New To Java 1 03-05-2009 09:53 PM
Help with really simple coding tigertomas New To Java 10 01-24-2009 05:47 AM
coding help accies76 New To Java 5 11-12-2008 09:15 PM
Help with program coding cachi AWT / Swing 1 07-31-2007 08:16 AM


All times are GMT +2. The time now is 06:37 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org