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

AI Thread Summary
The discussion centers around a Java program designed to find numbers above the average in an array of incomes. The user encounters issues with the output, specifically that the range of numbers above the average is not displayed correctly. The code calculates the average and counts how many numbers exceed it but fails to print the actual numbers above the average, instead displaying a memory address due to improper array printing. Suggestions include storing the above-average numbers in a separate array or string for proper output. Additionally, it is noted that the average calculation uses integer division, which may lead to inaccuracies; switching to floating-point division is recommended for more precise results.
Ajmather
Messages
2
Reaction score
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
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).
 
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].
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top