OneCompiler

Average me

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 Format
The first line contains N: numbers of tree.

Then follows N lines represents the height of each tree.

Output Format
Print the average height of all the trees in the yard

Example 1
Input

5
6
8
34
9
3

Output

12

Explanation

Sum of heights =60

Average of heights =60/5

Example 2
Input

3
1
2
3

Output

2

Constraints:

1 <= n <= 100000

1 <= a[i] <= 100000

 function Average(arr) {
 
}

var readline = require("readline").createInterface(process.stdin);

let inputArr = [];
var lineNumber = -1;
let size;
readline.on("line", readInputs);

function readInputs(line) {
  inputArr.push(line);
  lineNumber++;
   size=parseInt(inputArr[0]);
  if (lineNumber == size) {
    logic("s");
    readline.close();
  }
}

function logic(input) {
 

    let Arr = [];
    for(let i=1;i<=size;i++){
      Arr.push(parseInt(inputArr[i]));
    }
   
    console.log(Average(Arr));
  }



1 Answer

3 years ago by
 function Average(arr) {

	let sum =0;
	for(let i=0; i<arr.length; i++){
		sum+=arr[i];
	}

	// console.log(sum/5);
	return sum/arr.length;
}

var readline = require("readline").createInterface(process.stdin);

let inputArr = [];
var lineNumber = -1;
let size;
readline.on("line", readInputs);

function readInputs(line) {
  inputArr.push(line);
  lineNumber++;
   size=parseInt(inputArr[0]);
  if (lineNumber == size) {
    logic("s");
    readline.close();
  }
}

function logic(input) {
 

    let Arr = [];
    for(let i=1;i<=size;i++){
      Arr.push(parseInt(inputArr[i]));
    }
   
    console.log(Average(Arr));
  }



3 years ago by Anish Kumar