answersLogoWhite

0

Java-find average using array

Updated: 12/18/2022
User Avatar

Wiki User

14y ago

Best Answer

public static final double getAverage(final int[] ns) {

if (ns.length == 0) {

return 0.0;

}

int sum = 0;

for (int n : ns) {

sum += n;

}

return ((double) sum) / ns.length;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java-find average using array
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Queue ADT Using Array?

implement the queue ADT using an array


Why and is not using ' and ' in reading a string using scanf?

I guess you wanted to ask, why is it scanf ("%s", array)and not scanf ("%s", &array).Well, array is by definition a pointer to the first element: array = &array[0]


What is need to array?

Because using array you can easily access the data required


How do you display 15 numbers in an ascending order using an array?

Sort the array then traverse the array, printing the element values as you go.


How do you find particular element in array?

by using index position we can find the particular element in array.


Sort array in ascending descending order in python?

Using sorted(array,reverse=True)


How do you print element in array using javascript?

You would insert this command right after your array values have been specified.document.write(name of array[Number on array this item is, starts at 0])


How do you write a c plus plus program that will average 10 numbers entered and print the array?

#include<iostream> #include<sstream> #include<vector> using namespace std; int main() { vector<int> Array; for (size_t index=0; index<9; ++index) { int i; bool valid = false; while (!valid) { cout << "Enter a number: "; string input; cin >> input; stringstream ss; ss << input; valid = (ss >> i); if (!valid) cout << "Invalid input.\n"; } Array.push_back (i); } // print array and average int average=0; for (size_t index=0; index<9; ++index) { cout << Array[index] << ' '; average += Array[index]; } average /= 10; cout << "\nAverage: " << average << endl; }


How do you count the vowels in a string using PHP?

build an array of vowels then do a foreach on the array and then explode the string on the array value and the answer is -1 of the result


Can you implements tree using array?

yes you can....


Write an Algorithm to delete a last element from the array?

// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;


Write a c program to reverse an array without using another array?

public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i < array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }