+ 1

help sum of two array2d

public class MultArray { static public void sum(int[][] a, int[][] b){ int rows = a[0].length/2; int colums = a[1].length; int[][] soma = new int[rows ][colums ]; for(int i = 0; i < rows; i++){ for(int j = 0; i < colums ; j++){ soma[i][j]= a[i][j] + b[i][j]; } } System.out.println("sum of matrizes is : "); for(int[] index: soma){ for(int elementos: index){ System.out.print(elementos+ " "); } System.out.println(""); } } public static void main(String args[]) { int[][] first = { { 2, 3, 4,-6 }, { 5, 2, 3,0} }; int[][] second = { { -4, 5, 3,7 }, { 5, 6, 3,1 } }; sum(first , second ); } }

12th Oct 2022, 10:38 PM
Willy Almeida
Willy Almeida - avatar
3 Answers
+ 2
Second loop condition must be j<columns; instead of i<columns; typo? rows, columns may become wrong if you change dimentions..
13th Oct 2022, 8:10 AM
Jayakrishna 🇼🇳
+ 1
Jayakrishna thank you very much, solved
13th Oct 2022, 1:25 PM
Willy Almeida
Willy Almeida - avatar
0
You're welcome..
13th Oct 2022, 1:47 PM
Jayakrishna 🇼🇳