Results 1 to 13 of 13
Thread: Average, Max, Min & Range
 02292012, 12:51 AM #1Member
 Join Date
 Feb 2012
 Posts
 57
 Rep Power
 0
Average, Max, Min & Range
Hey everyone,
Completely new to java unless you count the 6 weeks of class I've already gotten through. I'm completely stumped on this program which (of course) is due tonight. Right now, it compiles fine but after I enter 2 sets of digits, even if I then enter "q", the run goes into an infinite loop.
I know my range is probably in the wrong place, but wanted to at least have it in the code while I'm trying to figure it out.
Please help.
Java Code:import java.util.Scanner; /** Write a program that reads a set of floatingpoint values. Ask the user to enter the values, then print: the average of the values the smallest of the values the largest of the values the range, that is the difference between the smallest and largest. Only prompt for the values once. */ public class AvgSmLgRg { public static void main (String [] args) { double sum = 0; int count = 0; double largest = 0; double smallest = 0; double range = 0; Scanner in = new Scanner(System.in); //Process values when sentinel is entered. System.out.println("Enter values. Use Q to finish."); while (in.hasNextDouble()) { double value = in.nextDouble(); { //Find average sum = sum + value; count++; double average = sum/count; if (value > largest); { largest = value; } if (value < smallest); { smallest = value; range = largest  smallest; } System.out.println("The average is: " + average); System.out.println("The largest is: " + largest); System.out.println("The smallest is: " + smallest); System.out.println("The range is: " + range); } } } }

Re: Average, Max, Min & Range
Big warning. This:
Java Code:if (value > largest); { largest = value; }
Java Code:if (value > largest) { ; // semicolon becomes an empty statement } // do the following *always* regardless of whether value is > largest // or not. { largest = value; }
Java Code:if (value > largest) { largest = value; }

Re: Average, Max, Min & Range
Also, why the curly brace here?
Java Code:double value = in.nextDouble(); { // *** why this curly brace?
 02292012, 01:03 AM #4Member
 Join Date
 Feb 2012
 Posts
 57
 Rep Power
 0
Re: Average, Max, Min & Range
Fubarable,
When I move the printlns out of the loop, I get the following error:
jGRASP exec: javac g AvgSmLgRg.java
AvgSmLgRg.java:48: error: cannot find symbol
System.out.println("The average is: " + average);
^
symbol: variable average
location: class AvgSmLgRg
1 error
jGRASP wedge2: exit code for process is 1.
jGRASP: operation complete.
Based off of both replies so far, I've updated the code like this:
Java Code:import java.util.Scanner; /** Write a program that reads a set of floatingpoint values. Ask the user to enter the values, then print: the average of the values the smallest of the values the largest of the values the range, that is the difference between the smallest and largest. Only prompt for the values once. */ public class AvgSmLgRg { public static void main (String [] args) { double sum = 0; int count = 0; double largest = 0; double smallest = 0; double range = 0; Scanner in = new Scanner(System.in); //Process values when sentinel is entered. System.out.println("Enter values. Use Q to finish."); while (in.hasNextDouble()) { double value = in.nextDouble(); //Find average sum = sum + value; count++; double average = sum/count; if (value > largest) { largest = value; } if (value < smallest) { smallest = value; range = largest  smallest; } } System.out.println("The average is: " + average); System.out.println("The largest is: " + largest); System.out.println("The smallest is: " + smallest); System.out.println("The range is: " + range); } }

Re: Average, Max, Min & Range
Well the error makes sense. You declare the average variable inside of the while loop, and so it is only visible inside of this block of code (the while loop). To allow it to be visible throughout the main method, you'll want to declare it in the main method, above the while loop. This is called a scoping problem since the scope of the average variable is currently limited to the while loop. After this loop, the variable goes "out of scope" and is no longer available.
 02292012, 01:12 AM #6Member
 Join Date
 Feb 2012
 Posts
 57
 Rep Power
 0
Re: Average, Max, Min & Range
That makes sense...even though "scoping" is in the next chapter of our book. GRR.
Would I need to declare it with the rest of the declarations before the "Scanner in" line?
I guess what doesn't make sense to me is the example our book has shows
while (in.hasNextDouble());
{
value = in.nextDouble();
Process value.
}
This made me assume everything had to be after the value statement.

Re: Average, Max, Min & Range
Your instructions make sense because where you declare the variable is not necessarily the same as where you use the variable. So again declare it before the while loop, but use it inside the while loop, and after the while loop.
 02292012, 01:39 AM #8Member
 Join Date
 Feb 2012
 Posts
 57
 Rep Power
 0
Re: Average, Max, Min & Range
Okay. I found another site that helped and I switched around the order of some things. However, the smallest isn't showing up when compiled and ran. Everything else seems to be working....so I don't get why the smallest isn't.
Here's my updated code:
Java Code:import java.util.Scanner; /** Write a program that reads a set of floatingpoint values. Ask the user to enter the values, then print: the average of the values the smallest of the values the largest of the values the range, that is the difference between the smallest and largest. Only prompt for the values once. */ public class AvgSmLgRg2 { public static void main (String [] args) { double smallest = 0; //smallest number double largest = 0; //largest number int count = 0; //Total # of values double sum = 0; //Sum of all values Scanner in = new Scanner(System.in); //Process values when sentinel is entered. System.out.println("Enter values. Use Q to finish."); while (in.hasNextDouble()) { double value = in.nextDouble(); //Find maximum if (value > largest) { largest = value; } //Find minimum if (value < smallest) { smallest = value; } //Find average count++; //Counts values sum += value; //Keeps running total } if (count > 0) { double average = sum / count; //Find range double range = largest  smallest; System.out.println("The average is: " + average); System.out.println("The largest is: " + largest); System.out.println("The smallest is: " + smallest); System.out.println("The range is: " + range); } else { System.out.println("You didn't input enough data"); } } }
Last edited by jmscarlet9; 02292012 at 01:46 AM. Reason: Updated code again

Re: Average, Max, Min & Range
 02292012, 01:49 AM #10
Re: Average, Max, Min & Range
A trick when searching for large and/or small values is to set the starting value at the opposite limit.
For largest, set its initial value to the smallest possible number.
For smallest ,set initial value to the largest possible number.
 02292012, 01:53 AM #11Member
 Join Date
 Feb 2012
 Posts
 57
 Rep Power
 0
Re: Average, Max, Min & Range
This just has an example of what I'm trying to do (minus the range part).  Java: Example: Console: Min, max, average
I think I figured out why the smallest isn't working. When I did a test input of negative numbers, the "largest" appeared not to work. However, I think it's because I set it to initialize at 0. When I tried what this site suggested, e.g.,
Java Code:double smallest = Double.MIN_VALUE; //smallest number double largest = Double.MAX_VALUE; //largest number
jGRASP exec: java AvgSmLgRg2
Enter values. Use Q to finish.
2 3 4 q
The average is: 3.0
The largest is: 1.7976931348623157E308
The smallest is: 4.9E324
The range is: 1.7976931348623157E308
jGRASP: operation complete.
Any help on this last piece would be GREATLY appreciated.

Re: Average, Max, Min & Range
 02292012, 01:59 AM #13Member
 Join Date
 Feb 2012
 Posts
 57
 Rep Power
 0
Similar Threads

a range of numbers
By sh4rif in forum New To JavaReplies: 2Last Post: 12072011, 04:13 PM 
name in a range of names
By Evii0 in forum New To JavaReplies: 2Last Post: 04052011, 06:35 AM 
pls help in understanding the range of byte
By shivamskn in forum New To JavaReplies: 2Last Post: 06242010, 12:07 PM 
InRange adder
By filem_funk in forum New To JavaReplies: 1Last Post: 11272009, 01:06 AM 
Range within an Array
By End in forum New To JavaReplies: 6Last Post: 04182009, 06:53 PM
Bookmarks