Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote