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

Consider the following array: What is the length of the array? What is the range

ID: 3573435 • Letter: C

Question

Consider the following array: What is the length of the array? What is the range of indices (plural of index) in myArray? What is the value returned by typing myArray? How would you access the element stored at index 1 of myArray? What error occurs if you try to access an element that is not in an array's range? Write a method that accepts a 1-Dimensional array and computes the average value of the supplied array. The method should have the following characteristics: Uses the public modifier Is static Accepts a single parameter, a 1-Dimensional array of integers Returns a single double value corresponding to the average of all of the elements in the supplied array. You may assume that the supplied array is non-empty (has at least one element).

Explanation / Answer

Please follow the code and comments for description :

CODE :

public class averageCalc { // class to run the code

    public static void main(String[] args) { // driver method

        int array[] = {1, 5, 89, 65, 42, 13, 53, 28, 72, 36}; // array
        double res = avg(array); // call the method and save the result
        System.out.println("The Average of the Array is : " + res); // print to console
    }

    public static double avg(int[] arr) { // method that return the average
        double sum = 0, average; // local variables
        for (int i = 0; i < arr.length; i++) { // iterate over the length
            sum = sum + arr[i]; // sum the data
        }
        average = sum / arr.length; // calculate the average
        return average; // return the data
    }
}


OUTPUT :

The Average of the Array is : 40.4


Hope this is helpful.

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