1. Engineering
  2. Computer Science
  3. hi am wondering how i find the sum of items...

Question: hi am wondering how i find the sum of items...

Question details

Hi am wondering how I find the sum of items in a sub array without using inbuilt libaries or hard coding. I have attempted the question but I can't seem to get the code to pass the test. I am wondering what I am doing wrong

/**

     * 

     * @param idx

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

     * return 0 if index is invalid

     */

    public int sum(int idx) {

   

    if(idx < 0)

        return 0;

         

        int total = 0;

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

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

        total = total += idx[i];

        }

    return total;

       }

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