Write a C++ program to read two matrices with any size. Your program should have at least the following functions:
Below is the solution:
code:
#include<iostream>
#include<string.h>
using namespace std;
class matrix
{
//declare variable
private:
int row,col;
int a[10][10];
public:
matrix() //set the deafult contructor
{
row=0;
col=0;
}
matrix(int r,int c,int g[10][10]) //matric
constructor
{
row=r;
col=c;
for(r=0;r<row;r++)
for(c=0;c<col;c++)
a[r][c]=g[r][c];
}
void read() //read function
{
int r,c;
cout<<"\nInput the no. of
row:";
cin>>row;
cout<<"Input the no. of
column:";
cin>>col;
cout<<"Input the
elements:\n";
for(r=0;r<row;r++)
for(c=0;c<col;c++)
cin>>a[r][c];
}
int operator==(matrix m) //check for equal matrix
size
{
if(row==m.row &&
col==m.col)
return(1);
else
return(0);
}
matrix operator+(matrix m) //addition matrix function
operator overloading
{
int r,c;
int result[10][10];
for(r=0;r<row;r++)
for(c=0;c<col;c++)
result[r][c]=a[r][c]+m.a[r][c];
return(matrix(row,col,result));
}
matrix operator-(matrix m) //subtraction matrix
function operator overloading
{
int r,c;
int result[10][10];
for(r=0;r<row;r++)
for(c=0;c<col;c++)
result[r][c]=a[r][c]-m.a[r][c];
return(matrix(row,col,result));
}
matrix operator/(matrix m) //division matrix function
operator overloading
{
int r,c;
int result[10][10];
for(r=0;r<row;r++)
for(c=0;c<col;c++)
result[r][c]=a[r][c]/m.a[r][c];
return(matrix(row,col,result));
}
matrix operator*(matrix m) //multiplication matrix
function operator overloading
{
int r,c;
int result[10][10];
for(r=0;r<row;r++){
for(c=0;c<col;c++){
int sum=0;
for(int k=0;
k<row; k++){
sum += a[r][k] * m.a[k][c];
}
result[r][c]=sum;
}
}
return(matrix(row,col,result));
}
friend ostream&
operator<<(ostream& dout,matrix m); //function to print
the matrix
};
ostream& operator<<(ostream &dout,matrix m) //print
matrix
{
int r,c;
for(r=0;r<m.row;r++)
{
for(c=0;c<m.col;c++)
dout<<m.a[r][c]<<" ";
cout<<endl;
}
return(dout);
}
int main()
{
matrix m1,m2,m3,m4,m5,m6; //create object
cout<<"Input the data to matrix 1\n";
m1.read(); //read matrix1
cout<<"Input the data to matrix 2\n";
m2.read(); //read matrix2
if(m1==m2)
{
//matrix operation
m3=m1+m2;
m4=m1-m2;
m5=m1/m2;
m6=m1*m2;
cout<<"The m1 matrix is:\n";
cout<<m1<<endl;
cout<<"The m2 matrix is:\n";
cout<<m2<<endl;
cout<<"The resultsnt matrix after
addition is:\n";
cout<<m3<<endl;
cout<<"The resultsnt matrix after
subtraction is:\n";
cout<<m4<<endl;
cout<<"The resultsnt matrix after
division is:\n";
cout<<m5<<endl;
cout<<"The resultsnt matrix after
multiplication is:\n";
cout<<m6<<endl;
}
else
cout<<"There is no compatibility of
two m,atrix to add or subtract"<<endl;
return 0;
}
output:
Input the data to matrix 1
Input the no. of row:3
Input the no. of column:3
Input the elements:
1 2 3
1 2 3
1 2 3
Input the data to matrix 2
Input the no. of row:3
Input the no. of column:3
Input the elements:
1 2 3
1 2 3
1 2 3
The m1 matrix is:
1 2 3
1 2 3
1 2 3
The m2 matrix is:
1 2 3
1 2 3
1 2 3
The resultsnt matrix after addition is:
2 4 6
2 4 6
2 4 6
The resultsnt matrix after subtraction is:
0 0 0
0 0 0
0 0 0
The resultsnt matrix after division is:
1 1 1
1 1 1
1 1 1
The resultsnt matrix after multiplication is:
6 12 18
6 12 18
6 12 18
Write a C++ program to read two matrices with any size. Your program should have at...
Write a C program for the following: (i) (ii) (iii) (iv) (v) A function to read an NxM matrix (from console input). A function to display the NxM matrix on the screen. A function to add these two matrices. A function to subtract these two matrices. A function to multiply these two matrices. The program must also contain a main function that will first declare two 3x3 matrices A and B, allow input of data for A and B (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);...
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....
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...
Write a C program requesting the user to input two integer numbers and then requesting the user to either add, subtract, or multiply the two numbers (choose 0 to add, 1 to subtract, or 2 to multiply. Declares three separate functions for these choices to come after main(). Program needs to use a pointer to these three functions to perform the requested action. Must print to output.
Write a java program that will read the values for 3 matrices A, B, and C and store the result of their summation in matrix D. You need to use a method to read matrix values. Use another method to add the three 3x3 matrices (each is a 2 dimensional array with 3 rows and 3 columns). Finally, use a third method to print the value of the summation (matrix D). Write a test program that will cal the three...
You have two Matrices, A and B. Assume that they are the correct dimensions (i.e. Both rows and columns are equal). Write the MATLAB code to do the following. a) Add A and B b) Subtract B from A c) Multiply A and B d) Divide B by A e) A Cubed
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.
C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...
Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt and multiplies them together to obtain matrix C such that the element C(i,j) ofmatrix C is given by: C(i, j) = ∑A(i, k) ∗ B(k, j) where n = number of columns in A = number of rows in B. Display the matrix C in defaultMATLAB format. You cannot use the MATLAB matrix multiply or other inbuilt MATLAB functions forarithmetic operations. You must implement it....