0

I'm trying to write a method that takes a two-dimensional array of integers (t) and returns a one-dimensional array (sums) that holds the sum of all the elements for a particular row WITHOUT using arithmetic operators and using a for-each loop.

public static int sumArr(int[] m) { // Returns the sum of a row of a 1-D arr
        int sum = 0;
        for (int number : m) {
            sum = sum + number;
        }
        return sum;
    }

This is a separate method that returns the sum of a row within a 1-D array. As you can see, I use a for-each, however; I need to implicate this method in order to do the above task.

2
  • the sum of all the elements for a particular row WITHOUT using arithmetic operators - how would you calculate the sum without addition operator? Commented Mar 19, 2018 at 19:03
  • Use an explicit Iterator if you should not use a for-each loop Commented Mar 19, 2018 at 19:06

1 Answer 1

1

Simply:

int[][] t = ...
int[] sums = new int[t.length];
int i = 0;
for (int[] row : t) {
   sums[i++] = sumArray(row);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.