Idiom #195 Pass a two-dimensional array
Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).
/**
* @param {Array<Array>} arry
*
* @return {Array<Array>}
*/
function foo(arry) {
let len = 0;
let sum = 0;
arry.forEach(function(base, i) {
len += base.length;
base.forEach(function(a, j) {
sum += a * (i + 1) * (j + 1);
});
});
console.log('Array size:', arry.length, ',', len);
return sum;
}
foo(arry2d);
public static void foo(int[][] a){
System.out.println("Array a has a size of " + a.length+" by " + a[0].length);
int sum = 0;
for (int i = 0; i<a.length; i++){
for (int j = 0; j<a[0].length; j++){
sum += ((i+1)*(j+1)*a[i][j]);
}
}
System.out.println("The sum of all elements multiplied by their indices is: " + sum);
}