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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix(int sizeX, int sizeY);
Matrix(const Matrix &m); // copy constructor
~Matrix();
int GetSizeX() const { return dx; }
int GetSizeY() const { return dy; }
long &Element(int x, int y); // return reference to an element
void Print() const;
long &operator()(int x, int y); // () operator overloading
Matrix &operator=(const Matrix &m); // = operator overloading
friend ostream &operator<<(ostream &out, const Matrix &m); // << operator overloading
friend Matrix operator+(Matrix m1, Matrix m2); // binary + operator overloading
friend Matrix operator*(int fac, Matrix m1); // binary * operator overloading
friend Matrix operator*(Matrix m1, int fac); // binary * operator overloading
int dx, dy;
long **p;
};
#endif /* MATRIX_H */
#include <iostream>
#include <cassert>
//#include "Matrix.h"
using namespace std;
Matrix::Matrix(int sizeX, int sizeY) : dx(sizeX), dy(sizeY)
{
assert(sizeX > 0 && sizeY > 0);
p = new long*[dx];
// create array of pointers to long integers
assert(p != 0);
for (int i = 0; i < dx; i++)
{ // for each pointer, create array of long integers
p[i] = new long[dy];
assert(p[i] != 0);
for (int j = 0; j < dy; j++)
p[i][j] = 0;
}
}
Matrix::~Matrix()
{
for (int i = 0; i < dx; i++)
delete [] p[i]; // delete arrays of long integers
delete [] p; // delete array of pointers to long
}
long &Matrix::Element(int x, int y)
{
assert(x >= 0 && x < dx && y >= 0 && y < dy);
return p[x][y];
}
void Matrix::Print() const
{
for (int x = 0; x < dx; x++)
{
for (int y = 0; y < dy; y++)
cout << p[x][y] << "\t";
cout << endl;
}
}
Matrix::Matrix(const Matrix &m) : dx(m.GetSizeX()), dy(m.GetSizeY())
{
assert(m.GetSizeX() > 0 && m.GetSizeY() > 0); // quits if m.GetSize(X)(Y) is < 0
p = new long*[dx];
// create array of pointers to long integers
assert(p != 0);
for (int i = 0; i < dx; i++)
{ // for each pointer, create array of long integers
p[i] = new long[dy];
assert(p[i] != 0);
for (int j = 0; j < dy; j++)
p[i][j] = m.p[i][j];
}
}
//Overloads the () operator to get the elment
long& Matrix::operator()(int x, int y){
return this->Element(x, y);
}
//overloads the assignment operator
Matrix& Matrix::operator=(const Matrix &m){
this->dx = m.dx;
this->dy = m.dy;
//going row by row, copying values
for(int row = 0; row < this->dx; row++){
for(int col = 0; col < this->dy; col++){
this->p[row][col] = m.p[row][col];
}
}
return *this;
}
//prints out the matrix
ostream& operator<<(ostream &out, const Matrix &m){
m.Print();
return out;
}
//overloads the addition to add matries
Matrix operator+(Matrix m1, Matrix m2){
Matrix newMatrix(m1.dx, m1.dy);
for(int row = 0; row < m1.dx; row++){
for(int col = 0; col < m1.dy; col++){
newMatrix.Element(row, col) = m1.p[row][col] + m2.p[row][col];
}
}
return newMatrix;
}
//overloads the multiplication operator to multiply
// a matrix by a constant value
Matrix operator*(int fac, Matrix m1){
Matrix newMatrix(m1.dx, m1.dy);
for(int row = 0; row < m1.dx; row++){
for(int col = 0; col < m1.dy; col++){
newMatrix.Element(row, col) = fac * m1.p[row][col];
}
}
return newMatrix;
}
//overloads the multiplication operator to multiply
// a matrix by a constant value
Matrix operator*(Matrix m1, int fac){
Matrix newMatrix(m1.dx, m1.dy);
for(int row = 0; row < m1.dx; row++){
for(int col = 0; col < m1.dy; col++){
newMatrix.Element(row, col) = fac * m1.p[row][col];
}
}
return newMatrix;
}
////overloads the multiplication operator to multiply
// a matrix by another matrix.
Matrix operator*(Matrix m1, Matrix m2)
{
Matrix newMatrix(m1.dx, m2.dy);
for(int i = 0; i < newMatrix.dx; i++){
for(int j = 0; j < newMatrix.dy; j++){
int sum = 0;
for(int k = 0; k < m1.dx; k++){
sum += m1.p[k][j] * m2.p[i][k];
}
newMatrix.Element(i, j) = sum;
}
}
return newMatrix;
}
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <ctime>
//#include "Matrix.h"
using namespace std;
int main(int argc, char** argv) {
int size1, size2, size3;
const int RANGE = 2; //using to generate random number in the range[0,1]
cout << "Please input three positive integers to decide the sizes of matrices: ";
cin >> size1 >> size2 >> size3;
// Make sure each size is positive, otherwise abort the program
assert(size1 > 0 && size2 > 0 && size3 > 0);
// Declare Matrix objects with sizes passing in as parameter
// The system automatically calls the constructor of Matrix class, which takes two parameters
Matrix myMatrix1(size1, size2);
// Simulate the adjacency matrix for undirected graph
// Which only stores either 0 or 1 in the matrix
// where 0 represents no path between two vertices in the grpah
// 1 represents a path between two vertices in the graph
srand(time(0));
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
myMatrix1(i,j) = rand() % RANGE;
Matrix yourMatrix1 = myMatrix1;
Matrix hisMatrix1 = myMatrix1;
Matrix herMatrix1 = myMatrix1;
cout << "myMatrix1: " << endl;
cout << myMatrix1 << endl << endl;
cout << "yourMatrix1 = myMatrix1: " << endl;
cout << yourMatrix1 << endl << endl;
yourMatrix1 = 2 * myMatrix1;
hisMatrix1 = myMatrix1 + yourMatrix1;
herMatrix1 = myMatrix1 * 3;
// hisMatrix1 and herMatrix1 should be identical now, but still independent
cout << "myMatrix1: " << endl;
cout << myMatrix1 << endl << endl;
cout << "yourMatrix1 = 2 * myMatrix1: " << endl;
cout << yourMatrix1 << endl << endl;
cout << "hisMatrix1 = myMatrix1 + yourMatrix1: " << endl;
cout << hisMatrix1 << endl << endl;
cout << "herMatrix1 = myMatrix1 * 3: " << endl;
cout << herMatrix1 << endl << endl;
// herMatrix1 should be 4 * myMatrix1, and hisMatrix has not been changed
herMatrix1 = herMatrix1 + myMatrix1;
cout << "hisMatrix1: " << endl;
cout << hisMatrix1 << endl << endl;
cout << "herMatrix1 = herMatrix1 + myMatrix1: " << endl;
cout << herMatrix1 << endl << endl;
// Extra part of this Lab assignment
// You need to define the matrix multiplication in your definition of class Matrix
Matrix myMatrix2(size2,size3);
Matrix yourMatrix2(size2, size1);
Matrix hisMatrix2(size1, size3);
Matrix herMatrix2(size1, size1);
for (int i = 0; i < size2; i++)
{
for (int j = 0; j < size3; j++)
myMatrix2(i,j) = rand() % RANGE;
for (int j = 0; j < size1; j++)
yourMatrix2(i,j) = rand() % RANGE;
}
hisMatrix2 = myMatrix1 * myMatrix2;
herMatrix2 = yourMatrix1 * yourMatrix2;
cout << "myMatrix1: " << endl;
cout << myMatrix1 << endl << endl;
cout << "myMatrix2: " << endl;
cout << myMatrix2 << endl << endl;
cout << "hisMatrix2 = myMatrix1 * myMatrix2: " << endl;
cout << hisMatrix2 << endl << endl;
cout << "yourMatrix1: " << endl;
cout << yourMatrix1 << endl << endl;
cout << "yourMatrix2: " << endl;
cout << yourMatrix2 << endl << endl;
cout << "herMatrix2 = yourMatrix1 * yourMatrix2: " << endl;
cout << herMatrix2 << endl << endl;
cout << "herMatrix2's square: " << endl;
cout << herMatrix2*herMatrix2 << endl << endl;
return 0;
}
|