Java: Finding the numbers in an array above the average

In summary: Third, you should use System.out.println() to print the expected and actual behavior. The expected behavior is that the average of the income array is printed each time. The actual behavior is that the list of numbers above the average is printed.
  • #1
Ajmather
2
0
I am attempting to find the numbers above the average number of a set of numbers in an array. I am having trouble understanding why my output for the list of numbers above the average is so...weird. I'd appreciate the help!

My code:

Code:
public class Inequality 
{
	public static void main(String[] args) 
	{
        // list of incomes in thousands
		 int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21};
         int sum = 0;
         int average;
         int aboveAverage = 0;
         
         for (int i = 0;i< income.length;i++)
         {
        	 sum = sum + income[i];
         }
         average = sum/income.length;
         System.out.println("Average of income array: " + average);
         
         for (int i = 0; i < income.length; i++)
         {
        	 if (income[i]>average)
        	 {
        		 aboveAverage++;
        	 }
         }
         System.out.println("There are " + aboveAverage + " numbers above the average.");
         System.out.println("Range of numbers above the average: " + income);
	}
}

My output:

Code:
Average of income array: 89
There are 2 numbers above the average.
Range of numbers above the average: [I@15db9742
 
Technology news on Phys.org
  • #2
It appears to me that what you want to do is when you find a number is above the mean, store it into either an array or better yet, a comma-delimited string. I'm not familiar with Java, but it appear that when you simply attempt to print the income array, you are getting a memory address (like a pointer would behave in C).
 
  • #3
Ajmather said:
I am attempting to find the numbers above the average number of a set of numbers in an array.
And then do what with them: print them, find their product, or something else? The answer to this determines what data stricture, if any, you need to store these elements.

Ajmather said:
I am having trouble understanding why my output for the list of numbers above the average is so...weird.
There is a reason for a bug report template in many software products. You are supposed to write the expected behavior and the actual behavior. What seems weird to you may seem natural to another person.

First, I don't see where you print the list of numbers above the average. I see where you attempt to print the whole array. I agree with Mark that arrays are not supposed to be printed in this way, namely, using a single call to [m]System.out.println()[/m].

Second, currently you are using integer division in [m]sum/income.length[/m], which rounds down the result. You may want to use floating-point division by defining [m]average[/m] to be a [m]float[/m] or [m]double[/m] and using [m](float)sum/income.length[/m]. Alternatively, you can declare [m]sum[/m] as a [m]float[/m].
 

Related to Java: Finding the numbers in an array above the average

1. What is Java?

Java is a high-level, object-oriented programming language used for developing a wide variety of applications, from web and mobile applications to enterprise software and scientific computing.

2. What does "Finding the numbers in an array above the average" mean?

This refers to a common problem in programming where we need to find all the numbers in an array that are greater than the average value of the array. This can be useful for data analysis and processing tasks.

3. How do you find the average of an array in Java?

To find the average of an array in Java, you need to first calculate the sum of all the numbers in the array and then divide that sum by the total number of elements in the array. This will give you the average value of the array.

4. What is the most efficient way to find numbers above the average in an array?

The most efficient way to find numbers above the average in an array is to iterate through the array and compare each element to the average value. If the element is greater than the average, it can be added to a new array or printed out as a result.

5. Are there any built-in methods in Java for finding numbers above the average in an array?

No, there are no specific built-in methods in Java for finding numbers above the average in an array. However, there are many useful methods in the Java library that can be used to simplify the code and make the task easier, such as the Arrays class and the Math class.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
801
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
2
Views
706
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
793
Back
Top