第八章第五题(代数:两个矩阵相加)(Algebra: adding two matrices)

第八章第五题(代数:两个矩阵相加)(Algebra: adding two matrices)

  • 8.5(代数:两个矩阵相加)编写两个矩阵相加的方法。方法头如下:
    public static double[][] addMatrix(double[][] a,double[][] b)
    为了能够进行相加,两个矩阵必须具有相同的维数,并且元素具有相应或兼容的数据类型。假设c表示相加的结果矩阵,每个元素Cij就是aij+bij。例如,对于两个3x3的矩阵a和b,c有:
    在这里插入图片描述
    编写一个测试程序,提示用户输入两个3x3的矩阵,然后显示他们的和。下面是一个运行示例:
    Enter matrix1:1 2 3 4 5 6 7 8 9
    Enter matrix2:0 2 4 1 4.5 2.2 1.1 4.3 5.2
    The matrices are added as follows
    1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0
    4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2
    7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2
    8.5(Algebra: adding two matrices)Write the method of adding two matrices. The method header is as follows:
    public static double[][] addMatrix(double[][] a,double[][] b)
    In order to be able to add, the two matrices must have the same dimension and the elements must have corresponding or compatible data types. Suppose C is the result matrix of addition, and each element CIJ is AIJ + bij. For example, for two matrices A and B of 3X3, C has:
    在这里插入图片描述
    Write a test program that prompts the user to enter two 3x3 matrices, and then displays their sum. Here is a running example:
    Enter matrix1:1 2 3 4 5 6 7 8 9
    Enter matrix2:0 2 4 1 4.5 2.2 1.1 4.3 5.2
    The matrices are added as follows
    1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0
    4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2
    7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2

  • 参考代码:

    package chapter08;
    
    import java.util.Scanner;
    
    public class Code_05 {
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
    
            System.out.print("Enter matrix1:");
            double[][] matrix1=new double[3][3];
            for(int i=0;i<matrix1.length;i++){
                for (int j=0;j<matrix1[i].length;j++){
                    matrix1[i][j]=input.nextDouble();
                }
            }
    
            System.out.print("\nEnter matrix2:");
            double[][] matrix2=new double[3][3];
            for(int i=0;i<matrix2.length;i++){
                for (int j=0;j<matrix2[i].length;j++){
                    matrix2[i][j]=input.nextDouble();
                }
            }
    
            System.out.println("The matrices are added as follows");
            double[][] matrix=addMatrix(matrix1,matrix2);
            System.out.println(matrix1[0][0]+" "+matrix1[0][1]+" "+matrix1[0][2]+"    "
                    +matrix2[0][0]+" "+matrix2[0][1]+" "+matrix2[0][2]+"    "
                    +matrix[0][0]+" "+matrix[0][1]+" "+matrix[0][2]);
            System.out.println(matrix1[1][0]+" "+matrix1[1][1]+" "+matrix1[1][2]+" +  "
                    +matrix2[1][0]+" "+matrix2[1][1]+" "+matrix2[1][2]+" =  "
                    +matrix[1][0]+" "+matrix[1][1]+" "+matrix[1][2]);
            System.out.println(matrix1[2][0]+" "+matrix1[2][1]+" "+matrix1[2][2]+"    "
                    +matrix2[2][0]+" "+matrix2[2][1]+" "+matrix2[2][2]+"    "
                    +matrix[2][0]+" "+matrix[2][1]+" "+matrix[2][2]);
        }
    
        public static double[][] addMatrix(double[][] a,double[][] b){
            double[][] matrix=new double[3][3];
            for(int i=0;i<matrix.length;i++){
                for (int j=0;j<matrix[i].length;j++){
                    matrix[i][j]=a[i][j]+b[i][j];
                }
            }
            return matrix;
        }
    }
    
    
  • 结果显示:

    Enter matrix1:1 2 3 4 5 6 7 8 9
    
    Enter matrix2:0 2 4 1 4.5 2.2 1.1 4.3 5.2
    The matrices are added as follows
    1.0 2.0 3.0    0.0 2.0 4.0    1.0 4.0 7.0
    4.0 5.0 6.0 +  1.0 4.5 2.2 =  5.0 9.5 8.2
    7.0 8.0 9.0    1.1 4.3 5.2    8.1 12.3 14.2
    
    Process finished with exit code 0
    
    
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值