#java #matrix
#java #матрица
Вопрос:
public class matrixOperations {
public static void main(String args[]){
//lets create two matrix of dimentions 2*2
int x[][]={{1,2} , { 3, 4 }};
int y[][]={{1,4} , { 8,9 }};
int original[][] = new int[2][2];
// lets print original matrix x
System.out.println( " X matrix ");
for(int i=0;i<2;i ) {
for (int j = 0; j < 2; j ) {
System.out.print( x[i][j] " ") ;
}
System.out.println( " ");
}
System.out.println( " ");
System.out.println( " ");
// lets print original matrix Y
System.out.println( " Y matrix ");
for(int i=0;i<2;i ) {
for (int j = 0; j < 2; j ) {
System.out.print( y[i][j] " ") ;
}
System.out.println( " ");
}
System.out.println( " ");
System.out.println( " ");
//lets matrix sum be the result after the addition of matrix x and y ;
int sum[][] = new int[2][2]; //this will be 2*2 matrix
System.out.println( " addition matrix: X Y ");
for(int i=0;i<2;i ) {
for (int j = 0; j < 2; j ) {
sum[i][j] = x[i][j] y[i][j];
System.out.print(sum[i][j] " ");
}
System.out.println();//for generating new line after printing single row
}
System.out.println(" ");
System.out.println(" ");
// lets write code for matrix subtraction
// let matrix sub be the result after matrix subtraction
System.out.println( " subtraction matrix : X-Y ");
// sub matrix initialization
int sub[][] = new int [2][2];
for(int i=0;i<2;i ) {
for (int j = 0; j < 2; j ) {
sub[i][j] = x[i][j] - y[i][j];// only change
System.out.print(sub[i][j] " ");
}
System.out.println(" ");//for generating new line after printing single row
}
System.out.println(" ");
System.out.println(" ");
// let's write code for matrix multiplication
//let matrix mul be the result after matrix multiplication
System.out.println( " multiplication matrix X*Y : ");
int mul[][] = new int[2][2];
for(int i=0;i<2;i ){
for(int j=0;j<2;j )
{//initializing ith and jth element of mul matrix to be 0
mul[i][j]=0;
for(int k=0;k<2;k )
{
mul[i][j]= mul[i][j] x[i][k]*y[k][j];
}
// lets print the elements of the mul matrix
System.out.print(mul[i][j] " ");
}
System.out.println( " "); // here one row gets printed so need to change line
}
System.out.println(" ");
System.out.println(" ");
// now lets write code for matrix division
// here every x[i][j] element will be divided by corresponding
//y[i][j] element
System.out.println( " division matrix X/Y :");
for(int i = 0; i < 2; i ) {
for(int j = 0; j < 2; j ) {
System.out.print(( (double)x[i][j] /(double) y[i][j]) " ");
//here we need to typecast because integer division dosen't gaurentees to produce result as integer
}
System.out.println(" ");// to change line after printing each row
}
}
}
Результаты должны выглядеть следующим образом, мне просто нужно разбить код, который у меня есть, на 4 метода и основной. Я просто не знаю, как это сделать.
X матрица 1 2
3 4
Y матрица 1 4
8 9
матрица сложения: X Y 2 6 11 13
матрица вычитания: X-Y 0 -2
-5 -5
матрица умножения X * Y: 17 22
35 48
матрица деления X / Y: 1.0 0.5
0.375 0.4444444444444
Комментарии:
1. В какой части задачи вы не уверены?
2. Я действительно понял это! но спасибо