Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There are N trees in Terry's front yard. He is supposed to measures the height of each tree and find the average height of trees in his yard. What is the average height of a tree in Terry's front yard?

Note:- Print your answer as floor value (average height)
Input
The first line contains N: numbers of tree.
Then follows N lines represents the height of each tree.

Constraint:-
1 <= n <= 100000
1 <= a[i] <= 100000
Output
Print the average height of all the trees in the yard

What I have tried:

Java
<pre>import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
	public static void main (String[] args) {
                      // Your code here
		Scanner scan = new Scanner(System.in);
		int total_trees = scan.nextInt();
		int[] tree = new int[total_trees];
		for (int i=0; i< total_trees; i++) {
			tree[i] = scan.nextInt();
		}
		Arrays.sort(tree);
		int n = 0;
		int[] a = new int[total_trees];
		for(int i =0; i< total_trees; i++) {
			while(i < total_trees -1 && tree[i]==tree[i+1]){
				i++;
			}
				a[n] = tree[i];
				n++;
		}
		int sum = 0;
		for (int i=0; i<n; i++){
			sum += a[i];
		}
		int avg = (int)Math.floor((float)sum/n);
		System.out.println(avg);

	}
}
Posted
Updated 11-Dec-21 18:27pm
Comments
Patrice T 11-Dec-21 21:49pm    
And you have a question or problem with this code ?
A S Lokanadhan 11-Dec-21 21:54pm    
Is the program is correct? I have got some hidden test cases wrong. I'm doing it for an assignment. Can you help me?
Patrice T 11-Dec-21 22:17pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Quote:
Is the program is correct? I have got some hidden test cases wrong.

Depends on what you think.
Do you think these inputs should give same answer ?
3
1 11 21

12
1 1 1 1 1 1 1 1 1 1 11 21

12
1 11 21 21 21 21 21 21 21 21 21 21

I don't think so, but as far as I understand,it is what your program is doing.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Why do you sort the array? Averages don't depend on order.

What is the second array for???

Why do you need an array at all? All it takes is summing the input values.

Why are you using integers for heights, the problem statement wasn't clear about that issue; the input could well be:
5
1.1
2.2
3.3
4.4
5.5
 
Share this answer
 
Java
<pre>import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
	public static void main (String[] args) {
                      // Your code here
		Scanner scan = new Scanner(System.in);
		int total_trees = scan.nextInt();
		long[] tree = new long[total_trees];
		for (int i=0; i< total_trees; i++) {
			tree[i] = scan.nextInt();
		}
		
		long sum = 0;
		for (int i=0; i<total_trees; i++){
			sum += tree[i];
		}
		long avg = (long)Math.floor(sum/total_trees);
		System.out.println(avg);

	}
}
 
Share this answer
 
Comments
Richard MacCutchan 12-Dec-21 3:25am    
Why are you using two loops? You only need one, to add each height to the total. It is also polite to display a message before each scan call so the user knows what is being asked for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900