-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
107 lines (81 loc) · 2.46 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
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
/**
* Matrix.cpp
* Implements Matrix Class
*
* PRIVATE STUFF:
* unsigned int numCols
* unsigned int* matrix
**/
#include <iostream>
#include <stdexcept>
#include "Matrix.h"
using namespace std;
Matrix::Matrix(unsigned int _numCols){
if(_numCols <= 0)
throw domain_error("Matrix: the number of columns value is invalid");
numCols = (_numCols*(_numCols + 1))/2;
matrix = new unsigned int[numCols];
for(int i = 0; i < numCols; i++)
matrix[i] = INF;
}
Matrix::Matrix(const Matrix & src)
:numCols(src.getNumCols()){
matrix = new unsigned int[numCols];
for(int i = 0; i < numCols; i++){
matrix[i] = src.matrix[i];
}
}
Matrix::~Matrix(){
delete [] matrix;
}
unsigned int Matrix::getItem(unsigned int i, unsigned int j)const{
if (i < 0 || i > numCols)
throw domain_error("getItem: the i value is invalid");
if(j < 0 || j > numCols)
throw domain_error("getItem: the j value is invalid");
if(i < j){
int aux = i;
i = j;
j = aux;
}
if(i == j)
return 0;
return matrix[ ((i * (i - 1)) / 2) + j ];
}
void Matrix::setItem(unsigned int i, unsigned int j, unsigned int item){
if (i < 0 || i > numCols)
throw domain_error("setItem: the i value is invalid");
if(j < 0 || j > numCols)
throw domain_error("setItem: the j value is invalid");
if(item < 0)
throw domain_error("setItem: the item value is invalid");
if(i < j){
// cout << "setItem::Matrix --> entre al if: i < j" << endl;
int aux = i;
i = j;
j = aux;
}
if(i == j){
// cout << "setItem::Matrix --> entre al if: i == j" << endl;
return;
}
matrix[ ((i * (i - 1)) / 2) + j ] = item;
/*cout << "i: " << i << "j: " << j << endl
<< "setItem::Matrix --> " << "matrix[" << ((i * (i - 1)) / 2) + j << "]: "
<< matrix[ ((i * (i - 1)) / 2) + j ] << " = " << item << endl;
*/
}
unsigned int Matrix::getNumCols() const{
return numCols;
}
/*
int main(int argsc, char* agrsv[]){
Matrix m(8);
m.setItem(0,0,2);
m.setItem(4,6,67);
m.setItem(3,2,3);
cout << "item(0,0): " <<m.getItem(0,0) << endl
<< "item(4,6): " <<m.getItem(4,6) << endl
<< "item(3,2): " <<m.getItem(3,2) << endl;
return 0;
}*/