1. Engineering
  2. Computer Science
  3. how do i find the average of items in sub...

Question: how do i find the average of items in sub...

Question details

How do I find the average of items in sub arrays and return 0 if index is invalid without hardcoding and using inbuilt libaries. I have attempted the quest and I can seem to get the code to pass. I don't know what I am doing wrong.

   /**

     * 

     * @param idx

     * @return average of items in sub-array at index idx

     * return 0 if index is invalid

     */

    public double subsetAverage(int idx) {

   

    double sum = 0;

        if (idx < 0)

            return 0;

        for (int i = 0; i<data.length; i++)

            sum = sum + data[idx][i];

        return (double)sum/idx;

    }

 

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution