Code :
Matrix.java
import java.util.Scanner;
public class Matrix { //This class stores the matrix and provides functions for various operations
private int row; //stores
row count of matrix
private int column; //stores column count of
matrix
private float[][] array;
//stores the elements of matrix
public Matrix(int row, int column)
{ //parameterized constructor
this.row = row;
this.column =
column;
array=new
float[row][column]; //initialize the array to store
elements
}
public void fillMatrix() { //this function
takes input from user and fills the matrix
System.out.println("Please enter matrix elements :");
Scanner sc = new
Scanner(System.in);
for (int
i=0;i<this.row;i++) //loop over every row in
matrix
for (int j=0;j<this.column;j++) { //loop over every
column in matrix
this.array[i][j] = sc.nextFloat(); //take float value from
user
}
}
public void add(Matrix m) { //this function
performs addition operation on matrices
if (m==null ||
this.row!=m.row || this.column!=m.column) { //check if both
matrices are equal or not
System.out.println("Error: For addition matrices should be of equal
size!");
return; // return if matrices are not
equal
}
Matrix result = new
Matrix(this.row,this.column); //initialize the result
matrix
for (int
i=0;i<this.row;i++) //loop over every
row
for (int j=0;j<this.column;j++) { //loop over every
column
result.array[i][j] = this.array[i][j] + m.array[i][j]; //add
respective matrix elements
}
System.out.println("Result after addition:");
result.printMatrix(); // print the result
}
public void subtract(Matrix m) { //this
function performs subtraction operation on matrices
if (m==null ||
this.row!=m.row || this.column!=m.column) { //check if both
matrices are equal or not
System.out.println("Error: For subtraction matrices should be of
equal size!");
return; // return if matrices are not
equal
}
Matrix result = new
Matrix(this.row,this.column); //initialize the result
matrix
for (int
i=0;i<this.row;i++) //loop over every row in
matrix
for (int j=0;j<this.column;j++) { //loop over every
column in matrix
result.array[i][j] = this.array[i][j] - m.array[i][j]; //subtract
respective matrix elements
}
System.out.println("Result after subtraction:");
result.printMatrix(); // print the result
}
public void multiply(Matrix m)
{ //this function performs multiplication
operation on matrices
if (m==null ||
this.column!=m.row) { //check matrices size
System.out.println("Error: For multiplication first matrix column
number and second matrix row number should be of equal!");
return; //return if it doesn't meet the
requirement
}
Matrix result = new
Matrix(this.row,this.column); //initialize the result
matrix
for (int
i=0;i<this.row;i++) //loop over every row in
matrix
for (int j=0;j<this.column;j++) { //loop over every
column in matrix
float sum = 0.0f; //initialize
the sum to 0.0
for (int k=0;k<this.column;k++)
sum = sum + (this.array[i][k]*m.array[k][j]); //find
the product
result.array[i][j] = sum; //store the obtained product
from loop
}
System.out.println("Result after multiplication:");
result.printMatrix(); // print the result
}
public void printMatrix()
{ //this function prints the matrix size
and elements
System.out.printf("Matrix size: %d * %d\n",row,column); //print
size
for (int
i=0;i<this.row;i++) { //loop over
every row in matrix
for (int j=0;j<this.column;j++) { //loop over
every column in matrix
System.out.printf("%f ",this.array[i][j]); //print the
element
}
System.out.print("\n");
}
}
}
Main.java
import java.util.Scanner;
//This is the driver class
public class Main {
public static void main(String args[])
{ //Main function
Scanner sc = new
Scanner(System.in); //for user input
System.out.println("Please enter first matrix size:"); //input
first matrix
int row = sc.nextInt();
//row
int col = sc.nextInt();
//column
if (row<1||col<1)
throw new RuntimeException("Invalid operation!"); // row and column
can't be less than 1
Matrix m1 = new
Matrix(row,col); // create first matrix
m1.fillMatrix(); //fill first matrix
System.out.println("Please enter second matrix size:"); //input
second matrix
row = sc.nextInt();
//row
col = sc.nextInt();
//column
if (row<1||col<1)
throw new RuntimeException("Invalid operation!"); // row and column
can't be less than 1
Matrix m2 = new
Matrix(row,col); //create second matrix
m2.fillMatrix(); //fill second matrix
int x=1;
while(x!=0) {
displayMenu(); //display the operations
x = sc.nextInt(); //take user input
switch (x) {
case 1:
m1.add(m2); //perform addition
break;
case 2:
m1.subtract(m2); //perform subtraction
break;
case 3:
m1.multiply(m2); //perform multiplication
break;
case 4:
System.out.println("Please enter first matrix size:"); //input
first matrix
row = sc.nextInt(); //row
col = sc.nextInt(); //column
if (row<1||col<1) throw new RuntimeException("Invalid
operation!");
m1 = new Matrix(row,col); //create first matrix
m1.fillMatrix(); //fill first matrix
System.out.println("Please enter second matrix size:"); //input
second matrix
row = sc.nextInt(); //row
col = sc.nextInt(); //column
if (row<1||col<1) throw new RuntimeException("Invalid
operation!");
m2 = new Matrix(row,col); //create second matrix
m2.fillMatrix(); //fill second matrix
break;
case 0:
System.out.println("Thank you!"); //exit
x=0;
break;
}
}
}
public static void displayMenu() { //this
function displays various matrix operations
System.out.println("---Menu---");
System.out.println("1.
Addition");
System.out.println("2.
Subtraction");
System.out.println("3.
Multiplication");
System.out.println("4.
Create new matrices");
System.out.println("0.
Exit");
System.out.print("Enter
your option : ");
}
}
Output:
The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be...
The assignment In this assignment you will take the Matrix addition and subtraction code and modify it to utilize the following 1. Looping user input with menus in an AskUserinput function a. User decides which operation to use (add, subtract) on MatrixA and MatrixB b. User decides what scalar to multiply MatrixC by c. User can complete more than one operation or cancel. Please select the matrix operation 1- Matrix Addition A+ B 2-Matrix Subtraction A-B 3Scalar Multiplication sC 4-Cancel...
Write a C program to implement matrix multiplication operations. First, you need to prompt the user for the size of both matrix. Then take inputs for both matrices and perform matrix multiplication operation. Finally, print the resulting matrix into the output.
It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program...
Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each...
This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...
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);...
Please code in C++. link to continue the code is this below or you can make your own code if you wish(fix any mistakes if you think there are any in it): cpp.sh/3qcekv 3. Submit a header file (project3.h), a definition file (project3.cpp), a main file (main.cpp), and a makefile to compile them together. Make sure to run the command make and produce an application file and include it with your submission. For this project, you are required to create...
06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When the...
In this lab you will code a simple calculator. It need not be anything overly fancy, but it is up to you to take it as far as you want. You will be creating this program in three small sections. You have the menu, the performance of the basic arithmetic operations (Addition, Subtraction Multiplication, and Division), and the looping. You may want to get the switch menu working first, and then fill in the code for these four arithmetic operations...
Write a program which gets user choice for addition, subtraction, multiplication and division with characters ’+’, ’-’, ’*’, and ’/’ respectively. This program should keep asking for two numbers and a choice to perform such operations inside a loop which can be only stopped by entering ’q’. I've been working out of code blocks and now I'm not sure how to go about this iew Search Project Build Debug Fortran wwSmith Tools Tools+ Plugins DoxyBlocks Settings Help maino: int -NM...