1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
MATRIXMULT.CPP
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include "Matrix.h"
using namespace std;
int SetRows()
{
int num_rows;
cout << "Input the number of rows of the matrix." << endl;
cin >> num_rows;
return num_rows;
}
int SetCols()
{
int num_cols;
cout << "Input the number of columns of the matrix." << endl;
cin >> num_cols;
cout << "\n";
return num_cols;
}
int SetElements(int num_rows, int num_cols) //Set the number of elements to be the product of the number of rows and columns.
{
int num_elements = num_rows * num_cols;
return num_elements;
}
void New_Matrix(Matrix M) //Request information for a specific matrix.
{
cout << "Enter information for "<< M.m_name << ". \n";
}
void InstantiateMatrix(Matrix& M) //Input numerical values into our matrix.
{
cout << "Now input the values held in " << M.m_name << "." <<endl;
cout << "Input the elements row by row.\n" <<endl;
M.m_matrixElements.reserve(M.m_elements);
for (int h = 0 ; h < M.m_elements ; h++)
{
float element;
cin >> element;
M.m_matrixElements.push_back(element);
}
cout << endl;
}
void MatrixMultiplier(Matrix First, Matrix Second, Matrix &Product) // This is where the multiplication occurs.
{
for( int i = 0; i < First.m_rows ; i++)
for (int j = 0 ; j < Second.m_cols ; j ++)
for (int k = 0; k < Product.m_cols; k++)
Product.m_matrixElements[i*Product.m_cols + j] += First.m_matrixElements[i*First.m_cols+k]*Second.m_matrixElements[j+Second.m_cols*k];
}
int main()
{
cout << "\n\nWelcome to the matrix multiplier!\n" << endl; //Welcome, etc.
cout << "You will be asked to input some information" << endl;
cout << "about the matrices you wish to multiply and" << endl;
cout << "then the computer will make sure the matrice" << endl;
cout << "s are multiplicable. Then you will be return" << endl;
cout << "ed the product!\n" << endl;
Matrix A("Matrix A"), B("Matrix B"), C("Matrix C");
New_Matrix(A);
A.m_rows = SetRows(); // Here we initialize some important data members of matrix A.
A.m_cols = SetCols();
A.m_elements = SetElements(A.m_rows, A.m_cols);
New_Matrix(B);
B.m_rows = SetRows();
B.m_cols = SetCols();
B.m_elements = SetElements(B.m_rows, B.m_cols);
assert ( A.m_cols == B.m_rows); // Make sure the matrices can be multiplied before anything else.
InstantiateMatrix(A); // Here the matrices are filled with numbers.
InstantiateMatrix(B);
C.m_elements = A.m_cols*B.m_rows; //Set dimensions for matrix C, the product matrix, and reserve some memory for it.
C.m_rows = A.m_rows;
C.m_cols = B.m_cols;
C.m_matrixElements.reserve(C.m_elements);
void MatrixMultiplier(A, B, C); // Use the MatrixMultiplier function.
for (int i = 0; i < C.m_elements ; i++) //Here we output the product matrix.
{
cout << C.m_matrixElements[i] << " ";
if (( i + 1)% C.m_cols == 0)
cout << endl;
}
}
MATRIX.H
#ifndef Matrix_Def
#define Matrix_Def
#include <vector>
#include <string>
using namespace std;
class Matrix
{
public:
string m_name; //name the matrix
int m_rows; //give it a number of rows, columns, and elements.
int m_cols;
int m_elements;
vector<float> m_matrixElements; // This is the container for the matrix elements
Matrix(string name); // This is our constructor.
};
#endif
MATRIXSTUFF.CPP
#include <string>
#include "Matrix.h"
using namespace std;
Matrix::Matrix(string name) : m_name(name) {} // Our constructor. Name the matrix.
|