Reputation: 137
I am sorry, I am not sure if the title is correct, if it is not I will correct it once someone tells me what this is called. As you can understand I am new to programming...
I want to accomplish the following: I have a cycle:
for(int i=0; i < this.matrix.length; i++)
I will have a matrix for example like this:
1, 2, 2
2, 2, 3
0, 1, 2
I want to multiply the diagonal elements 1*2*2 I know how to get those elements each step of the cycle, but how can I used a temp variable, that every step will be multiplied by the new element? or is this not possible?
For example i make a variable:
double temp;
and each cycle step I want the new value to multiply by the old, but keeping the value, not sure if I am explaining this well. But if we use this matrix i would want something like this:
temp = 1;
next step it
temp = 2;
next step
temp = 4;
I tried doing this myself but in the end would get the wrong results, I understand I am doing the multiplication wrong, because when i changed the 2 2 element of the matrix to 3 instead of 2 my end result would be 9 instead of 6. I am sorry if this is badly explained...
Upvotes: 1
Views: 458
Reputation: 881
As your question title says , you wanna moltiply with recursion .
The possibility of a function to call itself is known as recursion.
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] array= {
{1, 2, 2},
{2, 2, 3},
{0, 1, 2}
};
if(array[0].length==array.length) // check if numbers of columns == rows
System.out.println("Result "+multiD(0, array));
else
System.out.println("No matrix NxN");
}
public static int multiD(int pos, int [][] m) {
if (pos == m.length ) {//get out after the last element (multiply for 1 )
return 1;
} else
if ( m[pos][pos] == 0 ) {// get out if we found a 0 value , so we don't need to go forward
return 0;
} else
return m[pos][pos] * multiD(pos+1,m); //calculate the result
}
Assuming you have a matrix "m" 1000x1000 and the m[0][0] is 0 , you will iterate 1000 times when the result is known at the beginning . To prevent this you should write something likethis :
(Missing in the other answers)
int result = 1;
for (int i=0; i<matrix.length; i++) {
result *= matrix[i][i];
if(result == 0)
break;
}
Upvotes: 0
Reputation: 1658
Here we go:
public static void main (String[] args) throws Exception {
int[][] matrix = new int[3][];
matrix[0] = new int[] {1, 2, 2};
matrix[1] = new int[] {2, 2, 3};
matrix[2] = new int[] {0, 1, 2};
int result = 1;
for (int i=0; i<matrix.length; i++) {
result *= matrix[i][i];
}
System.out.println(result);
}
Edit: If you want also to include right-to-left:
public static void main (String[] args) throws Exception {
int[][] matrix = new int[3][];
matrix[0] = new int[] {1, 2, 2};
matrix[1] = new int[] {2, 2, 3};
matrix[2] = new int[] {0, 1, 2};
int resultL2R = 1;
int resultR2L = 1;
for (int i=0; i<matrix.length; i++) {
resultL2R *= matrix[i][i];
resultR2L *= matrix[i][matrix.length-1-i];
}
System.out.println("left-to-right: " + resultL2R);
System.out.println("right-to-left: " + resultR2L);
}
Upvotes: 2
Reputation: 32
Ok I think this code does what you want for the above matrix:
int temp=1;
for(int r=0; r<3; r++)//traversing through row
{
for(int c=0; c<3; c++)//traversing through column
{
if(r==c)// condition for diagonal
temp*=array[r][c];
}// c close
System.out.println("Multiplication value after row "+(r+1)+" = "+temp);
}// r close
Upvotes: 0
Reputation: 71
For Diagonal multiplication you can use below mentioned code
public static void main(String[] args) {
//2D Array
int a[][]={{1,2,3},{2,3,4},{3,4,5}};
int multiplier=1;
for(int i=0;i<a.length;i++){
multiplier=multiplier*a[i][i];
}
System.out.println(multiplier);
}
Upvotes: 1
Reputation: 722
You have to multiply in the loop for the [i][i] element
int[][] array= {
{1,2,2},
{2,2,3},
{0,1,2}
};
int result=1;
for ( int i = 0; i < array.length ; i++) {
result=result*(array[i][i]);
}
System.out.println("Result "+result);
Upvotes: 1
Reputation: 8676
I guess you would like to have the solution like this:
public static void main (String [] args)
{
int[][] matrix = new int[][] {
{1, 2, 2},
{2, 2, 3},
{0, 1, 2}
};
int result = 1;
for(int i = 0; i < matrix.length; i++){
result = result * matrix[i][i];
}
System.out.println("Result: " + result);
}
Since you're declaring the result
variable before you get into for
loop, it will preserve the value evaluated inside the loop.
Upvotes: 1