import java.util.Random;
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Class Matrix. File: Matrix.java
public class Matrix {
public static final int MAX = 20;
//Attributes
private int size;
private int[][] table;
/**
* Default constructor
* Initializes the table with size MAX
*/
public Matrix() {
this.table = new int[MAX][MAX];
}
/**
* Parameterized constructor
* Initializes the table with size s
*/
public Matrix(int s) {
this.size = s;
this.table = new int[this.size][this.size];
}
/**
* Returns the size of the matrix
* @return
*/
public int getSize() {
return this.size;
}
/**
* The the number at position (r, c)
* @param r
* @param c
* @return
*/
public int getElement(int r, int c) {
return this.table[r][c];
}
/**
* Set a number at position (r, c)
* @param r
* @param c
* @param value
*/
public void setElement(int r, int c, int value) {
this.table[r][c] = value;
}
/**
* Initializes the matrix with random numbers
* @param low
* @param up
*/
public void init(int low, int up) {
Random rand = new Random();
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
setElement(r, c, rand.nextInt(up) + low);
}
}
}
/**
* Prints the matrix
*/
public void print() {
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
System.out.printf(\" %2s\", getElement(r, c));
}
System.out.printf(\"\ \");
}
}
/**
* Adds this matrix with Matrix a
* @param a
* @return
*/
public Matrix add(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = this.getElement(r, c) + a.getElement(r, c);
result.setElement(r, c, value);
}
}
return result;
}
/**
* Subtracts this matrix with Matrix a
* @param a
* @return
*/
public Matrix subtract(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = this.getElement(r, c) - a.getElement(r, c);
result.setElement(r, c, value);
}
}
return result;
}
/**
* Multiplies this matrix with Matrix a
* @param a
* @return
*/
public Matrix multiply(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = 0;
for (int k = 0 ; k < this.size ; k++) {
value += this.getElement(r, k) * a.getElement(k, c);
}
result.setElement(r, c, value);
}
}
return result;
}
/**
* Multiplies this matrix with a constant whatConst
* @param a
* @return
*/
public Matrix multiplyConst(int whatConst) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, getElement(r, c) * whatConst);
}
}
return result;
}
/**
* Transposes this matrix
* @param a
* @return
*/
public Matrix transpose() {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, this.getElement(c, r));
}
}
return result;
}
/**
*
* @param a
* @return
*/
p.
1. import java.util.Random;
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Class Matrix. File: Matrix.java
public class Matrix {
public static final int MAX = 20;
//Attributes
private int size;
private int[][] table;
/**
* Default constructor
* Initializes the table with size MAX
*/
public Matrix() {
this.table = new int[MAX][MAX];
}
/**
* Parameterized constructor
* Initializes the table with size s
*/
public Matrix(int s) {
this.size = s;
this.table = new int[this.size][this.size];
}
/**
* Returns the size of the matrix
* @return
*/
public int getSize() {
return this.size;
}
/**
* The the number at position (r, c)
* @param r
* @param c
2. * @return
*/
public int getElement(int r, int c) {
return this.table[r][c];
}
/**
* Set a number at position (r, c)
* @param r
* @param c
* @param value
*/
public void setElement(int r, int c, int value) {
this.table[r][c] = value;
}
/**
* Initializes the matrix with random numbers
* @param low
* @param up
*/
public void init(int low, int up) {
Random rand = new Random();
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
setElement(r, c, rand.nextInt(up) + low);
}
}
}
/**
* Prints the matrix
*/
public void print() {
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
System.out.printf(" %2s", getElement(r, c));
}
3. System.out.printf(" ");
}
}
/**
* Adds this matrix with Matrix a
* @param a
* @return
*/
public Matrix add(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = this.getElement(r, c) + a.getElement(r, c);
result.setElement(r, c, value);
}
}
return result;
}
/**
* Subtracts this matrix with Matrix a
* @param a
* @return
*/
public Matrix subtract(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = this.getElement(r, c) - a.getElement(r, c);
result.setElement(r, c, value);
}
}
return result;
}
/**
4. * Multiplies this matrix with Matrix a
* @param a
* @return
*/
public Matrix multiply(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = 0;
for (int k = 0 ; k < this.size ; k++) {
value += this.getElement(r, k) * a.getElement(k, c);
}
result.setElement(r, c, value);
}
}
return result;
}
/**
* Multiplies this matrix with a constant whatConst
* @param a
* @return
*/
public Matrix multiplyConst(int whatConst) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, getElement(r, c) * whatConst);
}
}
return result;
}
/**
* Transposes this matrix
* @param a
* @return
*/
5. public Matrix transpose() {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, this.getElement(c, r));
}
}
return result;
}
/**
*
* @param a
* @return
*/
public int trace() {
int trace = 0;
for (int d = 0 ; d < this.size ; d++) {
trace += this.getElement(d, d);
}
return trace;
}
/**
* Checks if this Matrix is equal to Matrix a
* @param a
* @return
*/
public boolean equals(Matrix a) {
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
if(this.getElement(r, c) != a.getElement(r, c))
return false;
}
}
return true;
}
/**
6. * Copies Matrix a to this matrix
* @param a
*/
public void copy(Matrix a) {
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
this.setElement(r, c, a.getElement(r, c));
}
}
}
/**
* Returns a copy of this matrix
* @return
*/
public Matrix getCopy() {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, this.getElement(r, c));
}
}
return result;
}
}// close class Matrix
import java.util.InputMismatchException;
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Client for class Matrix. File: MatrixClient.java
import java.util.Scanner;
public class MatrixClient {
public static final int MAX = 20;
public static final int LOW = 1;
public static final int UP = 10;
/**
* Clears the keyboard buffer
*
* @param input
7. */
public static void clearBuf(Scanner input) {
input.nextLine();
}
/**
* Display the menu
*/
public static void displayMenu() {
System.out.println("Your options are: ");
System.out.println("-----------------");
System.out.println("1) Add 2 matrices");
System.out.println("2) Subtract 2 matrices");
System.out.println("3) Multiply 2 matrices");
System.out.println("4) Multiply matrix by a constant");
System.out.println("5) Transpose matrix");
System.out.println("6) Matrix trace");
System.out.println("7) Make a copy");
System.out.println("8) Test for equality");
System.out.println("0) EXIT");
System.out.print("Please enter your option: ");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice = 0; // operation to be executed from menu
int numCommands = 0; // display counter
int size = 0; // for subarray processing
int value; // multiply matrix by this constant
// Get size of the matrix
while (true) {
System.out.println("Enter the size of the square matrix: ");
try {
size = input.nextInt();
} catch (InputMismatchException ime) {
System.out.println("INPUT ERROR!!! Invalid input. Please enter a positive
integer");
}
8. if ((1 > size) || (size > MAX))
System.out.println("INPUT ERROR!!! Invalid size. Positive and <= " + MAX);
else
break;
}
// Create matrices
Matrix first = new Matrix(size);
Matrix second = new Matrix(size);
Matrix result = new Matrix(size);
// Generate Matrix
first.init(LOW, UP);
second.init(LOW, UP);
// Start testing
while (true) {
displayMenu();
choice = input.nextInt();
numCommands += 1;
switch (choice) {
case 0: // Exit
// Close scanner
input.close();
System.out.println("Testing completed.");
System.exit(0);
break;
case 1: // Matrix Addition
System.out.println("First matrix is:");
first.print();
System.out.println("Second matrix is:");
second.print();
System.out.println("The resulting matrix is:");
result = first.add(second);
result.print();
break;
case 2: // Matrix Subtraction
System.out.println("First matrix is:");
9. first.print();
System.out.println("Second matrix is:");
second.print();
System.out.println("The resulting matrix is:");
result = first.subtract(second);
result.print();
break;
case 3: // Matrix Multiplication
System.out.println("First matrix is:");
first.print();
System.out.println("Second matrix is:");
second.print();
System.out.println("The resulting matrix is:");
result = first.multiply(second);
result.print();
break;
case 4: // Matrix Multiplication by a constant
System.out.print("Enter the multiplication constant: ");
value = input.nextInt();
System.out.println("The original matrix is:");
first.print();
System.out.println("The resulting matrix is:");
result = first.multiplyConst(value);
result.print();
break;
case 5: // Transpose a matrix
System.out.println("The original matrix is:");
first.print();
System.out.println("The resulting matrix is:");
result = first.transpose();
result.print();
break;
case 6: // Trace of a matrix
System.out.println("The original matrix is:");
first.print();
System.out.println("The trace for this matrix is: " + first.trace());
10. break;
case 7: // Make a copy
System.out.println("The original matrix is:");
first.print();
System.out.println("The copy of this matrix is:");
result = first.getCopy();
result.print();
System.out.println("Testing for equality. Should be equal!!");
if(first.equals(result))
System.out.println("The matrices are equal!!");
else
System.out.println("The matrices are NOT equal!!");
break;
case 8: // Test for equality
System.out.println("First matrix is:");
first.print();
System.out.println("Second matrix is:");
second.print();
if(first.equals(second))
System.out.println("The matrices are equal!!");
else
System.out.println("The matrices are NOT equal!!");
break;
default:
System.out.println("Inavlid choice. Please try again!!!");
}
System.out.println("Command number " + numCommands + " completed.");
// Clear keyboard buffer
clearBuf(input);
System.out.println();
}
}
}
SAMPLE OUTPUT:
11. Enter the size of the square matrix:
2
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 1
First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The resulting matrix is:
14 6
10 7
Command number 1 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 2
12. First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The resulting matrix is:
2 -4
6 -5
Command number 2 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 3
First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The resulting matrix is:
50 46
50 46
Command number 3 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
13. 3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 4
Enter the multiplication constant: 10
The original matrix is:
8 1
8 1
The resulting matrix is:
80 10
80 10
Command number 4 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 5
The original matrix is:
8 1
8 1
The resulting matrix is:
8 8
1 1
Command number 5 completed.
Your options are:
14. -----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 6
The original matrix is:
8 1
8 1
The trace for this matrix is: 9
Command number 6 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 7
The original matrix is:
8 1
8 1
The copy of this matrix is:
8 1
8 1
Testing for equality. Should be equal!!
The matrices are equal!!
15. Command number 7 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 8
First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The matrices are NOT equal!!
Command number 8 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 0
Testing completed.
Solution
16. import java.util.Random;
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Class Matrix. File: Matrix.java
public class Matrix {
public static final int MAX = 20;
//Attributes
private int size;
private int[][] table;
/**
* Default constructor
* Initializes the table with size MAX
*/
public Matrix() {
this.table = new int[MAX][MAX];
}
/**
* Parameterized constructor
* Initializes the table with size s
*/
public Matrix(int s) {
this.size = s;
this.table = new int[this.size][this.size];
}
/**
* Returns the size of the matrix
* @return
*/
public int getSize() {
return this.size;
}
/**
* The the number at position (r, c)
* @param r
* @param c
17. * @return
*/
public int getElement(int r, int c) {
return this.table[r][c];
}
/**
* Set a number at position (r, c)
* @param r
* @param c
* @param value
*/
public void setElement(int r, int c, int value) {
this.table[r][c] = value;
}
/**
* Initializes the matrix with random numbers
* @param low
* @param up
*/
public void init(int low, int up) {
Random rand = new Random();
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
setElement(r, c, rand.nextInt(up) + low);
}
}
}
/**
* Prints the matrix
*/
public void print() {
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
System.out.printf(" %2s", getElement(r, c));
}
18. System.out.printf(" ");
}
}
/**
* Adds this matrix with Matrix a
* @param a
* @return
*/
public Matrix add(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = this.getElement(r, c) + a.getElement(r, c);
result.setElement(r, c, value);
}
}
return result;
}
/**
* Subtracts this matrix with Matrix a
* @param a
* @return
*/
public Matrix subtract(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = this.getElement(r, c) - a.getElement(r, c);
result.setElement(r, c, value);
}
}
return result;
}
/**
19. * Multiplies this matrix with Matrix a
* @param a
* @return
*/
public Matrix multiply(Matrix a) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
int value = 0;
for (int k = 0 ; k < this.size ; k++) {
value += this.getElement(r, k) * a.getElement(k, c);
}
result.setElement(r, c, value);
}
}
return result;
}
/**
* Multiplies this matrix with a constant whatConst
* @param a
* @return
*/
public Matrix multiplyConst(int whatConst) {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, getElement(r, c) * whatConst);
}
}
return result;
}
/**
* Transposes this matrix
* @param a
* @return
*/
20. public Matrix transpose() {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, this.getElement(c, r));
}
}
return result;
}
/**
*
* @param a
* @return
*/
public int trace() {
int trace = 0;
for (int d = 0 ; d < this.size ; d++) {
trace += this.getElement(d, d);
}
return trace;
}
/**
* Checks if this Matrix is equal to Matrix a
* @param a
* @return
*/
public boolean equals(Matrix a) {
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
if(this.getElement(r, c) != a.getElement(r, c))
return false;
}
}
return true;
}
/**
21. * Copies Matrix a to this matrix
* @param a
*/
public void copy(Matrix a) {
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
this.setElement(r, c, a.getElement(r, c));
}
}
}
/**
* Returns a copy of this matrix
* @return
*/
public Matrix getCopy() {
Matrix result = new Matrix(this.size);
for (int r = 0 ; r < this.size ; r++) {
for (int c = 0 ; c < this.size ; c++) {
result.setElement(r, c, this.getElement(r, c));
}
}
return result;
}
}// close class Matrix
import java.util.InputMismatchException;
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Client for class Matrix. File: MatrixClient.java
import java.util.Scanner;
public class MatrixClient {
public static final int MAX = 20;
public static final int LOW = 1;
public static final int UP = 10;
/**
* Clears the keyboard buffer
*
* @param input
22. */
public static void clearBuf(Scanner input) {
input.nextLine();
}
/**
* Display the menu
*/
public static void displayMenu() {
System.out.println("Your options are: ");
System.out.println("-----------------");
System.out.println("1) Add 2 matrices");
System.out.println("2) Subtract 2 matrices");
System.out.println("3) Multiply 2 matrices");
System.out.println("4) Multiply matrix by a constant");
System.out.println("5) Transpose matrix");
System.out.println("6) Matrix trace");
System.out.println("7) Make a copy");
System.out.println("8) Test for equality");
System.out.println("0) EXIT");
System.out.print("Please enter your option: ");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice = 0; // operation to be executed from menu
int numCommands = 0; // display counter
int size = 0; // for subarray processing
int value; // multiply matrix by this constant
// Get size of the matrix
while (true) {
System.out.println("Enter the size of the square matrix: ");
try {
size = input.nextInt();
} catch (InputMismatchException ime) {
System.out.println("INPUT ERROR!!! Invalid input. Please enter a positive
integer");
}
23. if ((1 > size) || (size > MAX))
System.out.println("INPUT ERROR!!! Invalid size. Positive and <= " + MAX);
else
break;
}
// Create matrices
Matrix first = new Matrix(size);
Matrix second = new Matrix(size);
Matrix result = new Matrix(size);
// Generate Matrix
first.init(LOW, UP);
second.init(LOW, UP);
// Start testing
while (true) {
displayMenu();
choice = input.nextInt();
numCommands += 1;
switch (choice) {
case 0: // Exit
// Close scanner
input.close();
System.out.println("Testing completed.");
System.exit(0);
break;
case 1: // Matrix Addition
System.out.println("First matrix is:");
first.print();
System.out.println("Second matrix is:");
second.print();
System.out.println("The resulting matrix is:");
result = first.add(second);
result.print();
break;
case 2: // Matrix Subtraction
System.out.println("First matrix is:");
24. first.print();
System.out.println("Second matrix is:");
second.print();
System.out.println("The resulting matrix is:");
result = first.subtract(second);
result.print();
break;
case 3: // Matrix Multiplication
System.out.println("First matrix is:");
first.print();
System.out.println("Second matrix is:");
second.print();
System.out.println("The resulting matrix is:");
result = first.multiply(second);
result.print();
break;
case 4: // Matrix Multiplication by a constant
System.out.print("Enter the multiplication constant: ");
value = input.nextInt();
System.out.println("The original matrix is:");
first.print();
System.out.println("The resulting matrix is:");
result = first.multiplyConst(value);
result.print();
break;
case 5: // Transpose a matrix
System.out.println("The original matrix is:");
first.print();
System.out.println("The resulting matrix is:");
result = first.transpose();
result.print();
break;
case 6: // Trace of a matrix
System.out.println("The original matrix is:");
first.print();
System.out.println("The trace for this matrix is: " + first.trace());
25. break;
case 7: // Make a copy
System.out.println("The original matrix is:");
first.print();
System.out.println("The copy of this matrix is:");
result = first.getCopy();
result.print();
System.out.println("Testing for equality. Should be equal!!");
if(first.equals(result))
System.out.println("The matrices are equal!!");
else
System.out.println("The matrices are NOT equal!!");
break;
case 8: // Test for equality
System.out.println("First matrix is:");
first.print();
System.out.println("Second matrix is:");
second.print();
if(first.equals(second))
System.out.println("The matrices are equal!!");
else
System.out.println("The matrices are NOT equal!!");
break;
default:
System.out.println("Inavlid choice. Please try again!!!");
}
System.out.println("Command number " + numCommands + " completed.");
// Clear keyboard buffer
clearBuf(input);
System.out.println();
}
}
}
SAMPLE OUTPUT:
26. Enter the size of the square matrix:
2
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 1
First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The resulting matrix is:
14 6
10 7
Command number 1 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 2
27. First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The resulting matrix is:
2 -4
6 -5
Command number 2 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 3
First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The resulting matrix is:
50 46
50 46
Command number 3 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
28. 3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 4
Enter the multiplication constant: 10
The original matrix is:
8 1
8 1
The resulting matrix is:
80 10
80 10
Command number 4 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 5
The original matrix is:
8 1
8 1
The resulting matrix is:
8 8
1 1
Command number 5 completed.
Your options are:
29. -----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 6
The original matrix is:
8 1
8 1
The trace for this matrix is: 9
Command number 6 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 7
The original matrix is:
8 1
8 1
The copy of this matrix is:
8 1
8 1
Testing for equality. Should be equal!!
The matrices are equal!!
30. Command number 7 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 8
First matrix is:
8 1
8 1
Second matrix is:
6 5
2 6
The matrices are NOT equal!!
Command number 8 completed.
Your options are:
-----------------
1) Add 2 matrices
2) Subtract 2 matrices
3) Multiply 2 matrices
4) Multiply matrix by a constant
5) Transpose matrix
6) Matrix trace
7) Make a copy
8) Test for equality
0) EXIT
Please enter your option: 0
Testing completed.