Write a method to compute the average of the elements of an int array and return
ID: 3666703 • Letter: W
Question
Write a method to compute the average of the elements of an int array and return the value as a double. The method header is below. The int array and the number of elements in the array are both passed as parameters to the method.
You can assume that:
numElements is the correct number of elements in the array
numElements is > 0
numElements could be less than the size of the array (meaning some array positions might not hold values to be included in the average)
array elements are filled sequentially starting at position 0
public double calculateAverage(int[] nums, int numElements)
Explanation / Answer
// methods to calculate average
public double calculateAverage(int[] nums, int numElements){
int total = 0;
for(int i=0; i<numElements; i++){
total = total + nums[i];
}
return (double)total/numElements;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.