Reputation: 9
I have a class Mold
with the following constructor
public Mold(int[][] mold){
this.mold = mold;
}
I also have a method sum()
which looks like this:
public int sum(){
int sum = 0;
for( int i = 0; i <matrix.length; i++) {
sum += matrix[i][i];
}
return sum;
}
What I am trying to do is compute the sum of all the elements in Mold
but I am unsure as to what to put in the sum += matrix[i][i];
part in place of 'i'. I might even be doing it the complete wrong way. I have been stuck on this for a while and I cannot seem to figure it out.
An example of the output I would like to get is: int[][] test1 = { { 10, 5 }, { 2, 8 } };
should give 25 one the sum method has been applied.
If someone could push me in the right direction that would be great! Many thanks,
Upvotes: 0
Views: 3942
Reputation: 2363
Think about what you are doing exactly and how to accomplish it.
Remember a 2d array is an array of arrays.
for( int i = 0; i <matrix.length; i++) {
sum += matrix[i][i];
}
`sum[i][i]` in this code is essentially doing this.
0 0
1 1
2 2
3 3
4 4
...
What you need it to be doing is
0 0
0 1
0 2
...
1 0
1 1
1 2
...
Think of it like coordinates X and Y!
this would be your answer.
for(int i = 0; i < matrix.length; i++){
for(int j = 0; z < matrix[j].length; j++{
sum += matrix[i][j];
}
}
Notice this line in particular. matrix[i].length
What if the array wasn't completely uniform??
for instance.
i
j[0][][][][][]
[1][]
[2][][][]
[3][][][][][][]
this still would able to iterate through.
Upvotes: 1
Reputation: 4228
You are doing this:
Supposing a bi-dimensional array of 3x3 elements these are their coordinates (row, column):
[0,0][0,1][0,2]
[1,0][1,1][1,2]
[2,0][2,1][2,2]
The way you are counting using only one loop and only one variable in the loop is doing this:
i = 0 (counting the elemen 0,0)
[*][ ][ ]
[ ][ ][ ]
[ ][ ][ ]
i = 1 (counting the elemen 1, 1)
[ ][ ][ ]
[ ][*][ ]
[ ][ ][ ]
i = 2 (counting the elemen 2,2)
[ ][ ][ ]
[ ][ ][ ]
[ ][ ][*]
You need to use a double loop to do the sum. Loop every row and for every row looped you have to loop for each column of that row.
Something like this:
for (i = 0; i < maxRows; i++){
for (j = 0; j < maxCols; j++{
//
}
}
Upvotes: 2
Reputation: 85779
In your current approach, you're just adding the elements in the diagonal (0,0), (1,1), (2,2). If handling an array of array, you can use the enhanced for
loop twice: one to loop over the elements in the array and another to loop over the elements in the array of the array:
int sum = 0;
for(int[] moldElement : mold) {
for(int element : moldElement) {
sum += element;
}
}
return sum;
Upvotes: 1
Reputation: 6332
Nest the for loop
public int sum(){
int sum = 0;
for( int i = 0; i <matrix.length; i++) {
for (int j = 0; k < matrix[i].length; j++){
sum += matrix[i][j];
}
}
return sum;
}
As someone said, you're only iterating over one dimension of the array. When you have n dimensions of an array, you need n loops.
Upvotes: 0
Reputation: 178263
The way you have it, you will only sum up positions [0][0]
, [1][1]
, [2][2]
, ..., [i][i]
and you will miss most of the elements.
You have 2 dimensions in your array, so you need 2 loops.
Create another for
loop inside your existing for
loop, with a different looping variable, j
. It will stop when it reaches the end of the current row's length: matrix[i].length
. When adding to sum
, use both looping variables: matrix[i][j]
.
Upvotes: 1
Reputation: 6476
do double for loops.
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
sum += matrix[i][j];
}
}
return sum;
Upvotes: 0