If arrays M and M + N are shown below, write a program in Java to find the array N.
[tex] \text{M} \: = \left[
\ \textless \ br /\ \textgreater \ \begin{array}{c c c}
\ \textless \ br /\ \textgreater \ - 1 & 0 & 2 \\
\ \textless \ br /\ \textgreater \ - 3 & - 1 & 6 \\
\ \textless \ br /\ \textgreater \ 4 & 3 & - 1
\ \textless \ br /\ \textgreater \ \end{array}
\ \textless \ br /\ \textgreater \ \right][/tex]
[tex] \text{and \: M+N} = \left[
\ \textless \ br /\ \textgreater \ \begin{array}{c c c}
\ \textless \ br /\ \textgreater \ - 6 & 9 & 4 \\
\ \textless \ br /\ \textgreater \ 4 & 5 & 0 \\
\ \textless \ br /\ \textgreater \ - 1 & - 2 & - 3
\ \textless \ br /\ \textgreater \ \end{array}
\ \textless \ br /\ \textgreater \ \right][/tex]
Answers
Here comes the approach for the problem.
public class FindMatrix{
public static void main(){
int matrix_M[][]={{-1,0,2},{-3,-1,6},{4,3,-1}};
int matrix_M_N[][]={{-6,9,4},{4,5,0},{1,-2,-3}};
int matrix_N[][]=new int[3][3];
System.out.println("Matrix M:");
displayMatrix(matrix_M);
System.out.println("\nMatrix M+N: ");
displayMatrix(matrix_M_N);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
matrix_N[i][j]=matrix_M_N[i][j]-matrix_M[i][j];
}
System.out.println("\nSo, Matrix N will be: ");
displayMatrix(matrix_N);
}
static void displayMatrix(int a[][]){
for(int i[]:a){
for(int j:i)
System.out.print(j+" ");
System.out.println();
}
}
}
- Here, we are provided with the matrices M+N and M. So, to get matrix N, we will subtract matrix M from matrix M+N.
See the attachment for output.
Answer:
public class KboatSubtractDDA
{
public static void main(String args[]) {
int arrM[][] = {
{-1, 0, 2},
{-3, -1, 6},
{4, 3, -1}
};
int arrSum[][] = {
{-6, 9, 4},
{4, 5, 0},
{1, -2, -3}
};
int arrN[][] = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arrN[i][j] = arrSum[i][j] - arrM[i][j];
}
}
System.out.println("Array N:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arrN[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
}
hope it's helpful