Question

In: Computer Science

CSCI 1302 Lab # 4 Multidimensional Arrays Write in Java Language!! Programming Exercise 8.5 (Algebra: add/subtract...

CSCI 1302 Lab # 4 Multidimensional Arrays

Write in Java Language!!

Programming Exercise 8.5 (Algebra: add/subtract two matrices)

Write a method to add/subtract two matrices. The header of the method is as follows:

public static double[][] addMatrix(double[][] a, double[][] b

or

public static double[][] subtractMatrix(double[][] a, double[][] b

In order to be add/subtract, 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 * 3 matrices a and b, c is

Write a test program that prompts the user to enter two 3 * 3 matrices and displays their sum or differece.

Programming Exercise 8.6 (Algebra: multiply two matrices)

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 cij is ai1 * b1j + ai2 * b2j + c + ain * bnj. For example, for two 3 * 3 matrices a and b, c is

where cij = ai1 * b1j + ai2 * b2j + ai3 * b3j. Write a test program that prompts the user to enter two 3 * 3 matrices and displays their product.

Solutions

Expert Solution

import java.util.Scanner;

public class AddMatrix {

    public static double[][] addMatrix(double[][] a, double[][] b) {
        double[][] result = new double[a.length][a[0].length];
        for(int i = 0; i < a.length; ++i) {
            for(int j = 0; j < a[i].length; ++j) {
                result[i][j] = a[i][j] + b[i][j];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double[][] matrix1 = new double[3][3];
        double[][] matrix2 = new double[3][3];
        System.out.print("Enter matrix1: ");
        for(int i = 0; i < matrix1.length; ++i) {
            for(int j = 0; j < matrix1[i].length; ++j) {
                matrix1[i][j] = in.nextDouble();
            }
        }
        System.out.print("Enter matrix2: ");
        for(int i = 0; i < matrix2.length; ++i) {
            for(int j = 0; j < matrix2[i].length; ++j) {
                matrix2[i][j] = in.nextDouble();
            }
        }
        double[][] matrix3 = addMatrix(matrix1, matrix2);
        System.out.println("\nThe addition of the matrices is");
        double minSum = 0;
        int minRow = 0;
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix1[i][j]);
            }
            if(i == 1) {
                System.out.printf(" + ");
            } else {
                System.out.printf("   ");
            }
            for(int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix2[i][j]);
            }
            if(i == 1) {
                System.out.printf(" = ");
            } else {
                System.out.printf("   ");
            }
            for(int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix3[i][j]);
            }
            double sum = 0;
            for(int j = 0; j < 3; ++j) {
                sum += matrix3[i][j];
            }
            if (i == 0 || sum < minSum) {
                minSum = sum;
            }
            System.out.printf("\n");
        }
    }
}

import java.util.Scanner;

public class MultiplyMatrix {

    public static double[][] multiplyMatrix(double[][] a, double[][] b) {
        double[][] result = new double[a.length][b[0].length];
        for(int i = 0; i < a.length; ++i) {
            for(int j = 0; j < b[i].length; ++j) {
                double total = 0;
                for(int k = 0; k < a[i].length; ++k) {
                    total += (a[i][k] * b[k][j]);
                }
                result[i][j] = total;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double[][] matrix1 = new double[3][3];
        double[][] matrix2 = new double[3][3];
        System.out.print("Enter matrix1: ");
        for(int i = 0; i < matrix1.length; ++i) {
            for(int j = 0; j < matrix1[i].length; ++j) {
                matrix1[i][j] = in.nextDouble();
            }
        }
        System.out.print("Enter matrix2: ");
        for(int i = 0; i < matrix2.length; ++i) {
            for(int j = 0; j < matrix2[i].length; ++j) {
                matrix2[i][j] = in.nextDouble();
            }
        }
        double[][] matrix3 = multiplyMatrix(matrix1, matrix2);
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix1[i][j]);
            }
            if(i == 1) {
                System.out.printf(" * ");
            } else {
                System.out.printf("   ");
            }
            for(int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix2[i][j]);
            }
            if(i == 1) {
                System.out.printf(" = ");
            } else {
                System.out.printf("   ");
            }
            for(int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix3[i][j]);
            }
            System.out.printf("\n");
        }
    }
}

import java.util.Scanner;

public class SubtractMatrix {

    public static double[][] subtractMatrix(double[][] a, double[][] b) {
        double[][] result = new double[a.length][a[0].length];
        for (int i = 0; i < a.length; ++i) {
            for (int j = 0; j < a[i].length; ++j) {
                result[i][j] = a[i][j] - b[i][j];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double[][] matrix1 = new double[3][3];
        double[][] matrix2 = new double[3][3];
        System.out.print("Enter matrix1: ");
        for (int i = 0; i < matrix1.length; ++i) {
            for (int j = 0; j < matrix1[i].length; ++j) {
                matrix1[i][j] = in.nextDouble();
            }
        }
        System.out.print("Enter matrix2: ");
        for (int i = 0; i < matrix2.length; ++i) {
            for (int j = 0; j < matrix2[i].length; ++j) {
                matrix2[i][j] = in.nextDouble();
            }
        }
        double[][] matrix3 = subtractMatrix(matrix1, matrix2);
        System.out.println("\nThe addition of the matrices is");
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix1[i][j]);
            }
            if (i == 1) {
                System.out.printf(" - ");
            } else {
                System.out.printf("   ");
            }
            for (int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix2[i][j]);
            }
            if (i == 1) {
                System.out.printf(" = ");
            } else {
                System.out.printf("   ");
            }
            for (int j = 0; j < 3; ++j) {
                System.out.printf("%5.1f\t", matrix3[i][j]);
            }
            System.out.printf("\n");
        }
    }
}

Related Solutions

Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] 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 * 3 matrices...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] 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 * 3 matrices...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Also please discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the following field: • ftemp: a double that holds a Fahrenheit temperature. The class should have the following methods : • Constructor : The constructor accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field. • setFahrenheit: The set Fahrenheit method accepts...
Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT