Advertisements
Advertisements
Question
If arrays Mand M + N are as shown below, write a program in Java to find the array N.
M = `[(-1, 0, 2),(-3, -1, 6),(4, 3, -1)]` M + N = `[(-6, 9, 4),(4, 5, 0),(1, -2, -3)]`
Answer in Brief
Solution
import java.util.*;
class AQ19{
public static void main(String args[]){
Scanner ob = new Scanner(System.in);
int M[][] = new int[3][3], N[][] = new int[3][3];
int MN[][] = new int[3][3);
int i, j;
// INPUT LOOP FOR MATRIX M
System.out.println("MATRIX M");
for (i = 0; i < 3; i++)
{System.out.println("Enter the elements in row " + i);
for (j = 0; j < 3; j++)
M[i][j] = ob.nextlnt();
}
// INPUT LOOP FOR MATRIX M+N
System.out.println("MATRIX M+N");
for (i = 0; i < 3; i++)
{System.out.printIn("Enter the elements in row " + i);
for (i = 0; j < 3; j++)
MN[i][j] = ob.nextInt();
}
// FINDING MATRIX N FROM MATRIX MN AND MATRIX M
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
N[i][j] = MN[i][j] - M[i][j];
}
// OUTPUT LOOP FOR MATRIX N
for (i = 0; i < 3; i++)
{System.out.println();
for (j = 0; j < 3; j++)
System.out.print(N[i][j] + "\t");
}
}}
shaalaa.com
Is there an error in this question or solution?
Chapter 3: Arrays (Single Dimensional and Double Dimensional) - EXERCISES [Page 243]