i am trying to calculate the sum of elements after the diagonal in 2D array but the problem is that the sum is always equal ZERO it do not change the answer.
where is the mistake in my code and how to fix it ?
i tried to use nested for loop and o tried to use one for loop but in both cases th eanswer still 0.
this is my code:
package test8;
import java.util.Scanner;
public class Question2 {
private int row = 4;
private int col = 4;
private int[][] matrix;
public Question2(int trow, int tcol) {
this.row = trow;
this.col = tcol;
}
public Question2(int trow, int tcol, int[][] m) {
this.row = trow;
this.col = tcol;
this.matrix = m;
}
public int[][] fill() {
int[][] data = new int[row][col];
Scanner in = new Scanner(System.in);
for (int row = 0; row < data.length; row++) {
for (int col = 0; col < data[row].length; col++) {
System.out.println("enter the elementss for the Matrix");
data[row][col] = in.nextInt();
}
System.out.println();
}
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < data[row].length; col++) {
System.out.print(data[row][col] + " ");
}
System.out.println();
}
return data;
}
public int calculate(int[][] num) {
int sum = 0;
for (int row = 0; row < num.length; row++) {
//for (int col = row + 1; col < num[row].length; col++) {
// if(row == col){
System.out.println(row);
sum += num[row][row];
// }
//}
}
System.out.println("the sum is: " + sum);
return sum;
}
public static void main(String[] args) {
Question2 q2 = new Question2(3, 3);
int[][] ma = q2.fill();
q2.calculate(ma);
}
}
this is the output:
1 2 3
4 5 6
7 8 9
0
1
2
the sum is: 15
Problem is that in your
fill
method you are creating localdata
array, which is filled with values and returned as result, but in yourmain
method you are not storing this array anywhere. Instead incalculate
you are usingint[][] ma = new int[3][3];
which is filled with0
.So maybe change your code to something like
Anyway your code seems strange. It looks like you want your class to store some array, but you are not using this array anywhere (except in
fill
method where you are using itslength
properties to filldata
array - which seems wrong sincedata
can have different size thanmatrix
).Maybe in
fill
method you shouldn't be creating newdata
array, but instead you should fillmatrix
array? Also in that case you don't need to pass this array as argument in constructor, but just create new array based onrow
andcol
values.This way you will not even need
calculate
method to get any array from outside, just usematrix
field.In other words your code can look more like
Now you can simply use
new Question2(3,3)
and be sure that it will also have empty array with correct sizes.Time for
fill
method. Don't create localint[][] data
array, just usematrix
instead, so changeto
Last thing is
calculate
method. You don't need it to actually take any array from user, because you already havematrix
array inside your class so you can simply reuse it. So instead ofcalculate(int[][] num)
make itcalculate()
and instead ofnum
usematrix
.So now your code can look like
You have multiple structure problem but, responding your question, replace this line:
by: