954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

possible loss of precision error for obtaining sum, average, etc..

[java=code]import java.util.Scanner;

public class AverageArray {
public static void main(String[] args) {
final int TOTAL_NUMBERS = 10;
int[] numbers = new int[TOTAL_NUMBERS];
float sum;

//Create a Scanner
Scanner input = new Scanner(System.in);

//Read all numbers
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number:");

//Conver String into integer
numbers[i] = input.nextInt();
}

//Find the sum and average

for (int i = 0; i < numbers.length; i++){
sum += numbers[i];

int average = sum / TOTAL_NUMBERS;

}

//Find the number of values in the array greater than the average
int maxAverage = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > average) maxAverage++;

}

//Prepare the result
String output = "The array is";
for (int i = 0; i < numbers.length; i ++) {
output += numbers[i] + " ";
}

output += "\nThe average is" + average;
output += "\nThe number of values in the array greater than the average" + "is" + maxAverage;

//Display the result
System.out.println(output);

}
}
[/CODE]

AverageArray.java:25: possible loss of precision
found : float
required: int
int average = sum / TOTAL_NUMBERS;
^
AverageArray.java:32: cannot find symbol

JRabbit2307
Light Poster
32 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
//Find the sum and average

          for (int i = 0; i < numbers.length; i++){
          sum += numbers[i];
          int average = sum / TOTAL_NUMBERS;
          }


Here you are making the average an integer, when in reality it should be a float so that you have the correct precision.

you can either:
a) change "int average = sum / TOTAL_NUMBERS;" to "float average = sum / TOTAL_NUMBERS;"
b) if you really want average to be rounded to an int, type cast the right side of the equals. "int average = (int) sum/TOTAL_NUMBERS;"

so what you should really have looks like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = sum / TOTAL_NUMBERS;


JamesCherrill:... and put line 5 outside the loop!

cant believe i let that slip. thanks james

Foxh451
Newbie Poster
4 posts since Aug 2009
Reputation Points: 10
Solved Threads: 2
 

... and put line 5 outside the loop!

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

JamesCherrill ok so i put line 5 outside the loop float average = sum / TOTAL_NUMBERS; i think that worked but now im still having issues getting the number of value greater than the average i think i totally have the wrong method...

[java=code]//Find the number of values in the array greater than the average
int maxAverage = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > average) maxAverage++;

}

[/CODE]

AverageArray.java:32: illegal start of type
for (int i = 0; i < numbers.length; i++) {
^
AverageArray.java:32: ')' expected
for (int i = 0; i < numbers.length; i++) {
^

JRabbit2307
Light Poster
32 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Not enough code to tell, but looks like a mis-matched bracket shortly before the line where you get the error.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

b) if you really want average to be rounded to an int, type cast the right side of the equals. "int average = (int) sum/TOTAL_NUMBERS;"

so what you should really have looks like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = sum / TOTAL_NUMBERS;

Casting is not a good idea for rounding numbers to int. If you use the code above (casting), it will not round your average to the nearest int but rather it will truncate the resulting number and drop the decimal places. A better and accurate way to do it is to use Math.round(float average) which will actually round your average to the nearest int.
Example:
let's say average = 5.9
by casting to int:
average = 5
by using Math.round:
average = 6

your code should be like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = Math.round(sum / TOTAL_NUMBERS);
Jocamps
Junior Poster
120 posts since Jun 2007
Reputation Points: 30
Solved Threads: 7
 

Seriously - if you can't figure out how to use code tags then read the stickies at the top of the forum that explain in detail how to do so. Or you could just quote one of the other members in here (such as Jocamps) who used code tags in their post, and it would show you exactly how to do it.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

you guys have been very helpful I've tried to work around this error but its not helping i dont see where i am getting an illegal start the brackets seem to be in place. but then i'm questionable on my formula of obtaining the number of values in array greater than the average any ideas???

}
   
   //Find the number of values in the array greater than the average 
   int maxAverage = 0;
   for (int i = 0; i < numbers.length; i++) {
      if (numbers[i] > average) maxAverage++;
      
   }

AverageArray.java:32: illegal start of type
for (int i = 0; i < numbers.length; i++) {
^
AverageArray.java:32: ')' expected
for (int i = 0; i < numbers.length; i++) {
^
AverageArray.java:32: illegal start of type

JRabbit2307
Light Poster
32 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

try posting your whole code

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

I tested your code that you posted above with the fix I suggested earlier and it is working fine. Perhaps you accidentally deleted or added an extra bracket. Here it is though:

import java.util.Scanner;

public class AverageArray {
	public static void main(String[] args) {
		final int TOTAL_NUMBERS = 10;
		int[] numbers = new int[TOTAL_NUMBERS];
		float sum = 0;

		// Create a Scanner
		Scanner input = new Scanner(System.in);

		// Read all numbers
		for (int i = 0; i < numbers.length; i++) {
			System.out.print("Enter a number:");

			// Conver String into integer
			numbers[i] = input.nextInt();
		}

		// Find the sum and average

		for (int i = 0; i < numbers.length; i++) {
			sum += numbers[i];

		}
		float average = sum / TOTAL_NUMBERS;

		// Find the number of values in the array greater than the average
		int maxAverage = 0;
		for (int i = 0; i < numbers.length; i++) {
			if (numbers[i] > average)
				maxAverage++;

		}

		// Prepare the result
		String output = "The array is";
		for (int i = 0; i < numbers.length; i++) {
			output += numbers[i] + " ";
		}

		output += "\nThe average is" + average;
		output += "\nThe number of values in the array greater than the average"
				+ "is" + maxAverage;

		// Display the result
		System.out.println(output);

	}
}

Casting is not a good idea for rounding numbers to int. If you use the code above (casting), it will not round your average to the nearest int but rather it will truncate the resulting number and drop the decimal places. A better and accurate way to do it is to use Math.round(float average) which will actually round your average to the nearest int. Example: let's say average = 5.9 by casting to int: average = 5 by using Math.round: average = 6 your code should be like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = Math.round(sum / TOTAL_NUMBERS);

@jocamps
I recommended casting to JRabbit (if he wanted to use the integer option) because, in his code, his array is only storing integers. Where he is checking whether it's greater than average he has simply "if (numbers[i] > average)," so if he used Math.round his count will be off because he doesn't have "if (numbers[i] >= average)." If the average was 5.9 and you rounded, his code wouldn't count 6 as above average, which it is. So using his code, the casted version is actually what he would need. I suppose I should have recommended the floor function but that is a different story.

Foxh451
Newbie Poster
4 posts since Aug 2009
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You