Question

Exercise 3: Write a method to add two matrices. The header of the method is: I public static double[][] addMatrix(double [ ][

0 0
Add a comment Improve this question Transcribed image text
Answer #1

    import java.util.Scanner;
   
    class AddMat
    {
       public static void main(String args[])
       {
          int m, n, c, d;
          int s1=0,s2=0,s3=0;
          Scanner in = new Scanner(System.in);
       
          System.out.println("Enter the size of matrix");
          m = in.nextInt();
          n = in.nextInt();
       
          double first[][] = new double[m][n];
          double second[][] = new double[m][n];
          double sum[][] = new double[m][n];
       
          System.out.println("Enter the elements of first matrix");
       
          for (c = 0; c < m; c++)
             for (d = 0; d < n; d++)
                first[c][d] = in.nextDouble();
             
          System.out.println("Enter the elements of second matrix");
       
          for (c = 0 ; c < m ; c++)
             for (d = 0 ; d < n ; d++)
                second[c][d] = in.nextDouble();
             
          for (c = 0; c < m; c++)
             for (d = 0; d < n; d++)
                 sum[c][d] = first[c][d] + second[c][d];
   
          System.out.println("Sum of the matrices:");
       
          for (c = 0; c < m; c++)
          {
             for (d = 0; d < n; d++)
                System.out.print(sum[c][d]+"\t");
               System.out.println();
          }
          for (c = 0; c < m; c++)
          {
             for (d = 0; d < n; d++)
                s1+=sum[0][d];
                s2+=sum[1][d];
                s3+=sum[2][d];
          }
          if(s1<s2 && s1<s3)
          {
              System.out.println("Row 1 has minimum sum"+s1);
          }
          else if(s2<s1 && s2<s3)
          {
              System.out.println("Row 2 has minimum sum"+s2);
          }
          else
          {
              System.out.println("Row 3 has minimum sum"+s3);
          }
       }
    }

Add a comment
Know the answer?
Add Answer to:
Exercise 3: Write a method to add two matrices. The header of the method is: I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method to multiply two matrices. The header of the method is: public static double[][]...

    Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for...

  • Must be in Python(please have a screen shot on these solution code) 11.5 (Algebra: add two...

    Must be in Python(please have a screen shot on these solution code) 11.5 (Algebra: add two matrices) Write a function to add two matrices. The header of the function is def addMatrix(a, b): In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element Cij is aij + bij. For example, for two 3 X 3 matrices a and b, c is...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...

    MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and 3) multiplies them (if compatible) and prints their product. Prompt the user for the names of two matrix input files (see format below) and place the output in a file of the user’s choosing. The format of the input files should contain the number of rows, followed...

  • The data on the below shows the number of hours a particular drug is in the...

    The data on the below shows the number of hours a particular drug is in the system of 200 females. Develop a histogram of this data according to the following intervals: Follow the directions. Test the hypothesis that these data are distributed exponentially. Determine the test statistic. Round to two decimal places. (sort the data first) [0, 3) [3, 6) [6, 9) [9, 12) [12, 18) [18, 24) [24, infinity) 34.7 11.8 10 7.8 2.8 20 9.8 20.4 1.2 7.2...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • 3. Write a complete assembly language program to read two matrices (2-dim arrays) A and B...

    3. Write a complete assembly language program to read two matrices (2-dim arrays) A and B and display the resulting matrix C, which is the sum of A and B. Procedures must be used to organize the code.

  • Matlab answer only Form 1 6) [10 pts] Consider the following two matrices B=[S A 3...

    Matlab answer only Form 1 6) [10 pts] Consider the following two matrices B=[S A 3 1 5 -7 4 6-3] a) Obtain the sum of each row of matrix A. b) Use the size function to create a magic matrix P which has the same size as matrix A. c) Create a matrix called Q from the second column of matrix A d) Extract the four numbers in the lower left-hand corner of matrix A and create matrix R....

  • CASE 1-5 Financial Statement Ratio Computation Refer to Campbell Soup Company's financial Campbell Soup statements in...

    CASE 1-5 Financial Statement Ratio Computation Refer to Campbell Soup Company's financial Campbell Soup statements in Appendix A. Required: Compute the following ratios for Year 11. Liquidity ratios: Asset utilization ratios:* a. Current ratio n. Cash turnover b. Acid-test ratio 0. Accounts receivable turnover c. Days to sell inventory p. Inventory turnover d. Collection period 4. Working capital turnover Capital structure and solvency ratios: 1. Fixed assets turnover e. Total debt to total equity s. Total assets turnover f. Long-term...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT