Question

C++ must use header files and implementation files as separate files. I’ll need a header file,...

C++ must use header files and implementation files as separate files. I’ll need a header file,
implementation file and the main program file at a minimum. Compress these files into one
compressed file.

Make sure you have adequate documentation.

We like to manipulate some matrix operations. Design and implement a class named
matrixMagic that can store a matrix of any size.

1. Overload the addition, subtraction, and multiplication operations.

2. Overload the extraction (>>) and insertion (<<) operators to read in the matrix
and to display its output.

3. Your class must have private attributes such as: int **matrix; and variables for
the number of rows and number of columns of the matrix.

4. Addition and subtraction: You can only add 2 matrices of the same size. For
example, the first matrix can have 3 rows and 3 columns and the second matric
could have 3 rows and 2 columns. We cannot add or subtract matrixes of different
size. Check this constraint before performing addition and subtraction of
matrices.

5. Multiplication: If the size of the first matrix is m X n (m is the number of rows
and n is the number of columns) and the size of the second matrix is n X t (n is the
number of rows and t is the number of columns), matrix multiplication only works
if the number of columns in the first matrix (n) is the same as the number of rows
in the second matrix (n). For example, we can multiply a 2 x 3 and a 3 x 4 matrix,
but not a 2 x 3 and 4 x 2 matrix. Check this constraint before performing
multiplication of matrices.

6. Use a continuous menu to allow the user for selecting the desired operation. Ask
the user for the entries in the matrix and display the output.

NOTE: The purpose of this exercise is to practice with overloading of operators. The
actual computation of matrices should be trivial.
Start with one operation at a time. Do not try to build the entire program before
running and testing individual pieces. For example, overload the input operator to read a
matrix and test it. Then, overload the output operator and test it. And, then move on to
matrix operations starting with the simple addition and subtraction, before moving to the
matrix multiplication operation.
If matrix 1 contains:
1 3
2 5
And matrix 2 contains:
4 3
6 -1
Then, the result will be:
M1 + M2 =
5 6
8 4
M1 - M2 =
-3 0
-4 6
M1 * M2 =
22 0
38 1
You can use the above matrices to test your code.
Here is a possible sample run:
Enter data for this matrix:
1 3
2 5
Enter data for this matrix:
4 3
6 -1


Here is what you entered:
Matrix 1:
1 3
2 5
Matrix 2:
4 3
6 -1
Adding matrices
5 6
8 4
Subtracting matrices
-3 0
-4 6
multiplying matrices
22 0
38 1
Matrix multiplication: Here is how we get the 22 in row 0, columns 0
1*4 + 3*6 = 22 1*3+3*(-1) = 0
2*4+5*6 = 38 2*3+5*(-1) = 1
The first row of the first matrix is multiplied by the first column of the second matrix and
added together, to give you the first element of the new matrix, etc.
As you see, you need to create a running total for the inner loop. To get the 22, you’ll
have the total of m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]

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

Code :

MagicMatrix.cpp :

#include "header.h"

class magicMatrix

{

int **matrix,nrows=MAX,ncolmns=MAX;

public:

magicMatrix()

{

matrix = new int *[nrows]; // Create a dynamicmatrix with maximum elements

for (int count = 0; count < nrows; count++)

matrix[count] = new int [ncolmns];

}

  

friend istream& operator >>(istream& din,magicMatrix &m);

friend ostream& operator <<(ostream& dout,magicMatrix& m);// display matrix

  

magicMatrix operator+(magicMatrix b)// sum

{

magicMatrix temp;

if((nrows!=b.nrows)||(ncolmns!=b.ncolmns)) //a.nrows!=b.nrows

{

cout<<"\n\tMatrix Addition is not possible the result is incorrect\n\n";

temp.nrows=0;

temp.ncolmns=0;

}

else

{

temp.nrows=nrows;

temp.ncolmns=ncolmns;

}

for(int i=0;i<nrows;i++)

for(int j=0;j<ncolmns;j++)

temp.matrix[i][j]=matrix[i][j]+b.matrix[i][j];

return temp;

}

magicMatrix operator-(magicMatrix b)// subtraction

{

magicMatrix temp;

if((nrows!=b.nrows)||(ncolmns!=b.ncolmns)) //a.nrows!=b.nrows

{

cout<<"\n\tMatrix Subtraction is not possible the result is incorrect\n\n";

temp.nrows=0;

temp.ncolmns=0;

}

else

{

temp.nrows=nrows;

temp.ncolmns=ncolmns;

}

for(int i=0;i<nrows;i++)

for(int j=0;j<ncolmns;j++)

temp.matrix[i][j]=matrix[i][j]-b.matrix[i][j];

return temp;

  

}

magicMatrix operator*(magicMatrix b)

{

magicMatrix r;

if((nrows!=b.ncolmns)||(ncolmns!=b.nrows))

{

cout<<"\n\tMatrix Multiplication is not possible the result is incorrect\n\n";

r.nrows=0;

r.ncolmns=0;

}

else

{

r.nrows=nrows;

r.ncolmns=b.ncolmns;

}

for(int i=0;i<nrows;i++)

for(int j=0;j<ncolmns;j++)

r.matrix[i][j]=0;

for(int i=0;i<nrows;i++)

for(int j=0;j<b.ncolmns;j++)

for(int k=0;(k<ncolmns)||(k<b.nrows);k++)

r.matrix[i][j]+=matrix[i][k]*b.matrix[k][j];

return r;  

}

  

};

istream& operator >>(istream& din,magicMatrix& m)

{

cout<<"Enter the size\n";

din>>m.nrows>>m.ncolmns;

m.matrix = new int *[m.nrows];

for (int count = 0; count < m.nrows; count++)

m.matrix[count] = new int [m.ncolmns];

cout<<"Enter the elements\n";

for(int i=0;i<m.nrows;i++)

for(int j=0;j<m.ncolmns;j++)

din>>m.matrix[i][j];

  

return din;

}

  

  

ostream& operator <<(ostream& dout,magicMatrix& m)

{

for(int i=0;i<m.nrows;i++)

{

for(int j=0;j<m.ncolmns;j++)

{

dout<<m.matrix[i][j]<<"\t";

}

dout<<endl;

}

return dout;

}

int main()

{

magicMatrix M1,M2,M3;

int temp=1;

cout<<"Matrix M1\n" ;

cin>>M1;

  

cout<<"\nMatrix M2\n";

cin>>M2;

cout<<"\n Matrix M1 is : \n";

cout<<M1<<endl;

cout<<"\n Matrix M2 is : \n";

cout<<M2<<endl;

M3=M1+M2;

cout<<"The Addition is:\n";

cout<<M3<<endl;

M3=M1-M2;

cout<<"The Subtraction is:\n";

cout<<M3<<endl;

M3=M1*M2;

cout<<"The Multiplication is:\n";

cout<<M3<<endl;

return 0;

}

header.h

#include<iostream>

using namespace std;

#define MAX 10

Output :

Add a comment
Know the answer?
Add Answer to:
C++ must use header files and implementation files as separate files. I’ll need a header file,...
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
  • In C++ Design a class to perform various matrix operations. A matrix is a set of...

    In C++ Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...

    C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the following definition: matrix -size:int -mat:int** ---------------- +matrix(file:string) +~matrix() +operator +(add:matrix*):matrix & +operator-(sub:matrix*):matrix & +friend operator<<(out:ostream &, t:const matrix&):ostream & +displayRow(r:int):void The class variables are as follows: • size: the number of rows and columns in a square matrix • mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2 • matrix(file:string): The default constructor. It receives the...

  • Please code in C++. link to continue the code is this below or you can make...

    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...

  • 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);...

  • Implement a C++ class to model the mathematical operations of a matrix. Your class should include...

    Implement a C++ class to model the mathematical operations of a matrix. Your class should include the following functions. add() which adds two matrices: power() which raises the first matrix to power n: "" which returns true if both matrices are equal. You need to overload the C++ equality operator. A sample run follows. Enter the number of rows: 2 Enter the number of columns: 3 Enter the elements of matrix 1 row by row: 1 5 0 1 3...

  • 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...

  • Given a 2d matrix of size N x M (which must be read from a file...

    Given a 2d matrix of size N x M (which must be read from a file provided to you), transpose it into a M x N matrix where N is the number of rows and M is the number of columns. void transpose(int n, int m, const int A[n][m], int AT[m][n]); Write a main() program to test and demonstrate your function. Show your function works for, at minimum, 3x6 and 10x6 matrices.

  • Question 1 a. Define the following matrices in a script file (M-file), ? = ( 8...

    Question 1 a. Define the following matrices in a script file (M-file), ? = ( 8 9 10 11; 23 9 16 15 ;11 12 3 6; 1 2 8 9 ) ? = ( 2 21 7 15; 12 4 8 22; 23 9 5 13; 23 4 21 22) ℎ = (4 9 12 15) b. Add suitable lines of codes to the M-file to do the following. Each of the following points should be coded in only...

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