-->
当前位置:首页 > 题库

PROGRAMMING:matrix addition

Luz5年前 (2021-05-10)题库422
Find the result of matrix A + B, the program gives part of the code, please complete the whole program on the basis of the original code.

import java.util.Scanner;
//Matrix add C = a + B / / write method call
public class Main {
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int m = scan.nextInt();// Row of matrix
int n = scan.nextInt();// Column of matrix
//Create two-dimensional array a, row m, column n
//Call the method to input data for a
//Create two-dimensional array B, row m, column n
//Call method to input data for B
//Call method to find a + B
//Output results
}
public static void inputData(int[][] arr){
for(int i =0; ifor(int j=0; jarr[i][j] =scan.nextInt();
}
public static int[][] addMatrix(int[][]arr1,int[][]arr2){
int [][] c = new int[arr1.length][arr1[0].length];
for(int i =0; ifor(int j =0; jc[i][j] = arr1[i][j] + arr2[i][j];
return c;
}
public static void showResult(int[][] arr){
for(int i =0; ifor(int j =0; jSystem.out.print(" "+arr[i][j]);
System.out.println();
}
}
}

###Input format:
The first row input the number of rows and columns of the matrix through the keyboard
The second line inputs the elements of matrix a through the keyboard, separated by spaces
The third line enters the elements of matrix B through the keyboard, separated by spaces
###Output format:
The result of output matrix addition is that each row of output matrix has one row, and each element is preceded by a space
###Input example:
Here is a set of inputs. For example:
```in
2 3
1 2 3 4 5 6
2 3 4 5 6 7
```
###Output example:
The corresponding output is given here. For example:
```out
3 5 7
9 11 13
```







answer:If there is no answer, please comment