So I have a question that I can't figure out involving matrices. It goes as follows:
Complete the implementations for the class Matrix in the file Matrix.cpp. You should only modify
Matrix.cpp and submit only Matrix.cpp to CCLE.
• num rows represents the number of rows in the Matrix object;
• num cols represents the number of columns in the Matrix object;
• vector<vector<int> > values stores all the entries in the matrix.
• The default constructor creates a matrix class object with zero dimensions.
• The other constructor initializes a matrix using user supplied number of rows and number of columns,
and sets the entries of the matrix to be random integer numbers from 0 to 9.
• operator + is overloaded to perform matrix addition with two matrix objects.
Say A and B are two matrices. In order to be able to add them, they both have to have the same
number of rows and the same number of columns. For example, if A has 3 rows and 5 columns then
B has to also have 3 rows and 5 columns. If the dimensions of the two matrices do not match, print
“Dimensions don’t match!” to the console. Otherwise, perform matrix addition entry-wise and return
the result as a Matrix object.
• void print(ofstream& fout) const outputs a Matrix object to a file as requested by the user.
A sample output in the text file for 3 × 5 matrices defined in the main function is something like:
4 5 2 7 9
6 1 8 6 3
4 9 1 5 1
9 1 2 5 1
3 6 7 9 4
9 3 7 3 1
13 6 4 12 10
9 7 15 15 7
13 12 8 8 2
The following are the given files:
Matrix.h
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
|
//matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Matrix
{
public:
Matrix();
Matrix(int new_num_rows, int new_num_cols);
Matrix operator+ (const Matrix& M2) const;
void print(ofstream& fout) const;
private:
int num_rows;
int num_cols;
vector<vector<int> > values;
};
#endif
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include "Matrix.h"
int main()
{
srand(time(0));
Matrix A(3,5);
Matrix B(3,5);
Matrix C(3,5);
C = A+B;
ofstream fout;
fout.open("out.txt");
A.print(fout);
B.print(fout);
C.print(fout);
fout.close();
return 0;
}
|
Matrix.cpp
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
|
#include "Matrix.h"
Matrix::Matrix ()
{
num_rows = 0;
num_cols = 0;
}
// Put your code below:
Matrix::Matrix(int new_num_rows, int new_num_cols) {
vector<vector<int> > values(new_num_rows, vector<int>(new_num_cols));
for (int i = 0; i < new_num_rows; i++) {
for (int j = 0; j < new_num_cols; j++) {
int RANDNUM = rand() % 10;
values[i][j] = RANDNUM;
}
}
for (int i = 0; i < new_num_rows; i++) {
for (int j = 0; j < new_num_cols; j++) {
cout << values[i][j] << " ";
}
cout << "\n";
}
}
Matrix Matrix::operator+(const Matrix& M2) const {
if (num_rows != M2.num_rows || num_cols != M2.num_cols) {
cout << "Dimensions don't match!" << endl;
}
else {
Matrix new_mat(num_rows, num_cols);
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
new_mat.values[i][j] = values[i][j] + M2.values[i][j];
}
}
return new_mat;
}
}
void Matrix::print(ofstream& fout) const {
fout.open("Matrix.cpp");
}
|
I can only edit what comes under "Put your code below:" and nothing more. As you'll notice, what I have is almost useless. Any explanations on how to proceed would be much appreciated.