-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
80 lines (74 loc) · 1.74 KB
/
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
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
#include"Master.hpp"
#include<cassert>
#include<cmath>
// Allocate memory for new matrix using double pointer
// and copy entries into the matrix
Matrix::Matrix (const Matrix& otherMatrix)
{
mNumRows=otherMatrix.mNumRows;
mNumcols=otherMatrix.mNumcols;
mData= new double *[mNumRows];
for (int i=0; i<mNumRows; i++)
{
mData[i]= new double [mNumcols];
}
for(int i=0; i<mNumRows; i++)
{
for(int j=0; j<mNumcols; j++)
{
mData[i][j]=otherMatrix.mData[i][j];
}
}
}
//Constructor for matrix of given row and columns
//Memory allocation for matrix and
//Initialization of entries to 0.0
Matrix::Matrix (int numRows, int numCols)
{
assert(numRows > 0);
assert(numCols > 0);
mNumRows = numRows;
mNumcols = numCols;
mData = new double* [mNumRows];
for (int i=0; i < mNumRows; i++)
{
mData[i] = new double [mNumcols];
}
for (int i=0; i < mNumRows; i++ )
{
for (int j=0; j<mNumcols; j++)
{
mData[i][j] = 0.0;
}
}
}
//overwritten destructor to free memory
Matrix::~Matrix()
{
for (int i=0; i<mNumRows; i++)
{
delete [] mData[i];
}
delete [] mData;
}
//Method to get number of rows of matrix
int Matrix::GetNumberOfRows() const
{
return mNumRows;
}
//Method to get number of columns
int Matrix::GetNumberOfCols() const
{
return mNumcols;
}
//Overloading round brackets
//Uses 1-based indexing
//and a check on validity of index
double& Matrix::operator()(int i, int j)
{
assert(i > 0);
assert(i < mNumRows+1);
assert(j > 0);
assert(j < mNumcols+1);
return mData[i-1][j-1];
}