Question

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);
private:
int values[MAX_ROWS][MAX_COLS];
int numRows;
int numCols;
};
Every instantiation of MatrixType will give you a new matrix. Initially, in the constructor, both the number of rows and the number of columns are set to 0. This indicates that an empty matrix is created. Then, the user is asked to provide the number of rows, the number of columns and the value for each combination of row and column. To represent the matrices, you need to consider an array (Matrix[10]) of MatrixType. Initially, the length of this array, LenthMatrix, is = 0. The index of the array can be 0 to 9, i.e., there can be at most 10 matrices at any given time.
Note that the class provides two member functions, AddSubCompatible and MultCompatible, to allow the client to determine if the binary matrix operations (addition, subtraction and multiplication) are possible. For example, adding a matrix of size 3X4 with another matrix 3X7 is infeasible. You need to check such necessary preconditions for each operation. If an operation is not possible, you should provide appropriate error message to user.

An example of output:

Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 1 A matrix with 0X0 is created. Enter the number of rows and the number of columns:

3 4

Enter the values: 4 7 9 3 1 67 5 19 100 99 3 17 Thank you. You now have total 1 matrices in system.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 5 Enter the matrix number for printing: 1 The input is invalid.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 5 Enter the matrix number for printing: 0 The matrix 0 is: 4 7 9 3 1 67 5 19 100 99 3 17
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 2 Let you want to perform C=A+B. Enter A, B and C: 0 1 3 The operation is not compatible.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 1 A matrix with 0X0 is created. Enter the number of rows and the number of columns: 4 3 Enter the values: 9 17 15 23 11 25 12 7 19 17 16 12 Thank you. You now have total 2 matrices in system.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 1 A matrix with 0X0 is created. Enter the number of rows and the number of columns: 3 4 Enter the values: 7 19 13 6 5 27 51 17
10 13 20 15 Thank you. You now have total 3 matrices in system.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 1 A matrix with 0X0 is created. Enter the number of rows and the number of columns: 3 4 Enter the values: 17 19 13 6 53 127 1 0 18 103 220 5 Thank you. You now have total 4 matrices in system.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 2 Let you want to perform C=A+B. Enter A, B and C: 0 2 3 Matrices 0 and 2 are added and the result is stored in matrix 3.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 5 Enter the matrix number for printing: 3 The matrix 3 is: 11 26 22 9 6 84 56 36 110 112 23 32
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 3 Let you want to perform C=A-B. Enter A, B and C: 0 3 2 Matrix 3 is subtracted from matrix 0, and the result is stored in matrix 2.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 4 Let you want to perform C=A*B. Enter A, B and C: 0 3 1 The operation is not compatible.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 1 A matrix with 0X0 is created. Enter the number of rows and the number of columns: 3 3 Enter the values: 14 17 12
Page 4 of 7
1 7 6 0 29 8 Thank you. You now have total 5 matrices in system.
Enter the corresponding number for your desired operation: 1 – get a new matrix; 2 – add two matrices; 3 – subtract a matrix from another; 4 – multiply two matrices; 5 – print matrix; 6 – end the program 4 Let you want to perform C=A*B. Enter A, B and C: 0 1 4 Matrices 0 and 1 are multiplied, and the result is stored in matrix 4.

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

matrixType.h

#include <iostream>
using namespace std;

const int MAX_ROWS = 10;
const int MAX_COLS = 10;
class MatrixType
{
    public:
        MatrixType();
        void MakeEmpty();
        void SetSize(int rowsSize, int colSize);
        int GetCols();
        int GetRows();
        int GetItem(int rowNum, int colNum);     
        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);
    private:
        int values[MAX_ROWS][MAX_COLS];
        int numRows;
        int numCols;
};

main.cpp

//#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "matrixType.h"
using namespace std;

MatrixType::MatrixType()
{
   numRows = 0;
   numCols = 0;
   for (int i = 0; i < MAX_ROWS; i++)
       for (int j = 0; j < MAX_COLS; j++)
           values[i][j] = 0;
}
/*
MakeEmpty fucntion:
Clears the Matrix of all Data
Pre: Matrix exists and is size numRows by numCols
Post: Matrix is zero'd out
*/
void MatrixType::MakeEmpty()
{

   for (int i = 0; i < numRows; i++)
       for (int j = 0; j < numCols; j++)
           values[i][j] = 0;

}

/*
SetSize Function:
Allows the user to set the variables, numRows and numCols, pertaining to the size of the matrix
*/
void MatrixType::SetSize(int rowsSize, int colSize)
{
   numRows = rowsSize;
   numCols = colSize;
}


/*
GetCols function:
Returns the number of columns of this class to the user.
*/
int MatrixType::GetCols()
{
   return numCols;
}

/*
GetRows function:
Returns the number of rows of this class to the user.
*/
int MatrixType::GetRows()
{
   return numRows;
}

int MatrixType::GetItem(int rowNum, int colNum)
{
   return values[rowNum][colNum];
}

/*
StoreItem function:
Allows the user to set the value of a particular matrix cell
Pre: Matrix has already been created
Post: Item has been placed into the Matrix at the proper coordinates
*/
void MatrixType::StoreItem(int item, int row, int col)
{
   values[row][col] = item;
}

/*
AddSubCompatible Function:
Pre: Matrix has already been created and populated with Data
Post: Returns a true or false depending on the compatibility of the given matrix and that
of the current classe's matrix
*/

bool MatrixType::AddSubCompatible(MatrixType otherOperand)
{
   if (numRows == otherOperand.GetRows())
   {
       if (numCols == otherOperand.GetCols())
           return true;
   }
   return false;
}

/*
MultCompatible Function:
Pre: Matrix has already been created and populated with Data
Post: Returns a true or false depending on the compatibility of the given matrix and that
of the current classes' matrix
*/
bool MatrixType::MultCompatible(MatrixType otherOperand)
{
   if (numCols == otherOperand.GetRows())
       return true;

   return false;
}

/*
Add Function:
pre: the Matrices have been checked for compatibility
post: the result is returned by reference
*/
void MatrixType::Add(MatrixType otherOperand, MatrixType& result)
{
   for (int i = 0; i < numRows; i++)
   {
       for (int j = 0; j < numCols; j++)
       {
           result.StoreItem(values[i][j] + otherOperand.GetItem(i, j), i, j);
       }
   }
}

/*
Sub Function:
pre: the Matrices have been checked for compatibility
post: the result is returned by reference
*/
void MatrixType::Sub(MatrixType otherOperand, MatrixType& result)
{
   for (int i = 0; i < numRows; i++)
   {
       for (int j = 0; j < numCols; j++)
           result.StoreItem(values[i][j] - otherOperand.GetItem(i, j), i, j);
   }
}

/*
Mult Function:
pre: The matrices have been checked to see if they are compatible, the result Matrix is the proper size
post: the result is returned by reference
*/
void MatrixType::Mult(MatrixType otherOperand, MatrixType& result)
{
   result.MakeEmpty();         //ensure that the result matrix is empty.
   int temp = 0;               //temporary integter used for storing data

   for (int i = 0; i < numRows; i++)
   {
       for (int j = 0; j < otherOperand.GetCols(); j++)
       {
           for (int k = 0; k < numCols; k++)
           {
               temp += values[i][k] * otherOperand.GetItem(k, j);
           }
           result.StoreItem(temp, i, j);
           temp = 0;
       }
   }
}

/*
Print Function:
pre:
post: Prints out the matrix to the file
*/

void MatrixType::Print(ofstream& outfile)
{
   outfile << "\nMatrix of size: " << numRows << ", " << numCols << ": " << endl;
   for (int i = 0; i < numRows; i++)
   {
       for (int j = 0; j < numCols; j++)
       {
           outfile << values[i][j] << " ";
       }
       outfile << endl;
   }

}

void outputMenu(ofstream&);
void inputMatrix(MatrixType& Matrix);
void outputMatrix(MatrixType Matrix);


int main()
{
   MatrixType Matrices[10];
   int choices[4] = { 0 }, item = 0;                   //User input storage
   bool done = false;                                   //used for the main while loop.
   ofstream fout;                          
   fout.open("DataOut.txt");

  
   while (!done)
   {
       outputMenu(fout);
       cin >> item;
       choices[0] = item;

       if (item == 0 || item == 4)
       {
           cin >> item;
           choices[1] = item;
       }
       else if (item != 5 && item < 5)
       {
           for (int i = 1; i < 4; i++)
           {
               cin >> item;
               choices[i] = item;
           }
       }
      

       switch (choices[0])
       {
       case 0:
           cout << "Creating New Matrix in slot " << choices[1] << endl;
           fout << "Creating New Matrix in slot " << choices[1] << endl;
           inputMatrix(Matrices[choices[1]]);
           break;
       case 1:
           if (Matrices[choices[1]].AddSubCompatible(Matrices[choices[2]]))
           {
               cout << "Adding slots " << choices[1] << " and " << choices[2] << " solution in " << choices[3] << endl;
               fout << "Adding slots " << choices[1] << " and " << choices[2] << " solution in " << choices[3] << endl;
               Matrices[choices[3]].SetSize(Matrices[choices[1]].GetRows(), Matrices[choices[1]].GetCols());
               Matrices[choices[1]].Add(Matrices[choices[2]], Matrices[choices[3]]);
               cout << endl;
               fout << endl;
           }
           else
               cout << "\n\n***Matrices incompatible. Please select a different pair of Matrices.***\n\n";
               fout << "\n\n***Matrices incompatible. Please select a different pair of Matrices.***\n\n";
           break;
       case 2:
           if (Matrices[choices[1]].AddSubCompatible(Matrices[choices[2]]))
           {
               cout << "Subtracting slots " << choices[1] << " and " << choices[2] << " solution in " << choices[3] << endl;
               fout << "Subtracting slots " << choices[1] << " and " << choices[2] << " solution in " << choices[3] << endl;
               Matrices[choices[3]].SetSize(Matrices[choices[1]].GetRows(), Matrices[choices[1]].GetCols());
               Matrices[choices[1]].Sub(Matrices[choices[2]], Matrices[choices[3]]);
               cout << endl;
               fout << endl;
           }
           else
               cout << "\n\n***Matrices incompatible. Please select a different pair of Matrices.***\n\n";
               fout << "\n\n***Matrices incompatible. Please select a different pair of Matrices.***\n\n";
           break;
       case 3:
           if (Matrices[choices[1]].MultCompatible(Matrices[choices[2]]))
           {
               cout << "Multiplying slots " << choices[1] << " and " << choices[2] << " solution in " << choices[3] << endl;
               fout << "Multiplying slots " << choices[1] << " and " << choices[2] << " solution in " << choices[3] << endl;
               Matrices[choices[3]].SetSize(Matrices[choices[1]].GetRows(), Matrices[choices[2]].GetCols());
               Matrices[choices[1]].Mult(Matrices[choices[2]], Matrices[choices[3]]);
               cout << endl;
               fout << endl;
           }
           else
               cout << "\n\n***Matrices incompatible. Please select a different pair of Matrices.***\n\n";
               fout << "\n\n***Matrices incompatible. Please select a different pair of Matrices.***\n\n";
           break;
       case 4:
           outputMatrix(Matrices[choices[1]]);
           Matrices[choices[1]].Print(fout);
           break;
       case 5:
           cout << "\nExiting.\n";
           fout << "\nExiting.\n";
           done = true;
           break;
       default:
           cout << "\n\n***Input not Valid. Please input a valid option.***\n\n";
           fout << "\n\n***Input not Valid. Please input a valid option.***\n\n";
           break;
       };
       item = 0;
       for (int i = 0; i < 4; i++)
           choices[i] = 0;
   }

          
   fout.close();
   //system("PAUSE");
    return 0;
}


void outputMenu(ofstream& fout)
{
   cout << "\nChoice and Parameters(0-9):" << endl;
   cout << "0 New Matrix            Usage 0: <Matrix>\n";
   cout << "1 Add Matrices          Usage 1: <Matrix> <Matrix> <Matrix>\n";
   cout << "2 Subtract Matrices     Usage 2: <Matrix> <Matrix> <Matrix>\n";
   cout << "3 Multiply Matrices     Usage 3: <Matrix> <Matrix> <Matrix>\n";
   cout << "4 Print Matrices        Usage 4: <Matrix>\n";
   cout << "5 Quit                  Usage 5:\n";
   cout << "\n\nEnter choice and Parameters: ";
  
   fout << "\nChoice and Parameters(0-9):" << endl;
   fout << "0 New Matrix            Usage 0: <Matrix>\n";
   fout << "1 Add Matrices          Usage 1: <Matrix> <Matrix> <Matrix>\n";
   fout << "2 Subtract Matrices     Usage 2: <Matrix> <Matrix> <Matrix>\n";
   fout << "3 Multiply Matrices     Usage 3: <Matrix> <Matrix> <Matrix>\n";
   fout << "4 Print Matrices        Usage 4: <Matrix>\n";
   fout << "5 Quit                  Usage 5:\n";
   fout << "\n\nEnter choice and Parameters: ";
  
}

void inputMatrix(MatrixType& Matrix)
{
   int row = 0, col = 0, item = 0;

   cout << "Enter row and Column Size: ";
   cin >> row >> col;
   Matrix.SetSize(row, col);
  
   for (int i = 0; i < row; i++)
   {
       cout << "\nRow " << i << ": ";
       for (int j = 0; j < col; j++)
       {
           cin >> item;
           Matrix.StoreItem(item, i, j);
       }
   }
   cout << endl;
}

void outputMatrix(MatrixType Matrix)
{
   cout << "\nThe Matrix requested: \n";
   for (int i = 0; i < Matrix.GetRows(); i++)
   {
       cout << "Row " << i << ": ";
       for (int j = 0; j < Matrix.GetCols(); j++)
           cout << Matrix.GetItem(i, j) << " ";
       cout << endl;
   }
}

Add a comment
Know the answer?
Add Answer to:
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...
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
  • Write a C program for the following: (i) (ii) (iii) (iv) (v) A function to read...

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

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

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

  • The assignment In this assignment you will take the Matrix addition and subtraction code and modify...

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

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

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

  • The C++ program below contains a function that adds a specific row of two matrices and...

    The C++ program below contains a function that adds a specific row of two matrices and store the result in the corresponding row of a third matrix. This is done by a loop to invokes a function that adds a single row of the two matrices. By the end of the loop all rows should be added and stored in the result matrix. The rows are added sequentially one after the other. You are required to modify the program below...

  • For the program described below, document your code well. Use descriptive identifier names. Use s...

    For the program described below, document your code well. Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does. Write a program to perform operations on square matrices (i.e. equal number...

  • For the program described below, document your code well. Use descriptive identifier names. Use spaces and...

    For the program described below, document your code well. Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does. Write a program to perform operations on square matrices (i.e. equal number...

  • Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods...

    Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods described: 1. read: reads in a matrix from the command line 2. display: prints the matrix to the command line 3. getRows: returns the number of rows 4. getCols: returns the number of columns 5. set: sets the double value at a particular column/row position 6. get: returns the value stored at a particular column/row position 7. Plus: returns a new Matrix object that...

  • Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method...

    Create a new class MathOP2 (MathOP2.java) that Inherits from MathOP You need to add multiply method and divide method, both methods accepts two parameters and return a value. Create a test program with documentation to test all operators (at least 4) The TestMathOP should do the following      Enter the First number >> 5.5      Enter the Second Number >> 7.5      The sum of the numbers is 13      The subtract of the two numbers is -2.00      Do...

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