-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixClass.cpp
More file actions
66 lines (60 loc) · 2.25 KB
/
Copy pathmatrixClass.cpp
File metadata and controls
66 lines (60 loc) · 2.25 KB
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
#include<iostream>
class matrix {
/*
Main class for matrix, future will call for multi-class
Trying to limit external librarys to minimize any dependincies
matrix (int -> rows, int -> columns) -> matrix of rows*columns size
matrixFill () -> fills matrix going across columns then down rows incramentin by 1
matrixRead () -> std::cout's all values of the matrix
transpose () -> transposes matrix flipping rows with columns and returning that value
*/
public:
matrix(int rows, int columns) { //defined for building the matrix
rows++;
columns++;
matrix::rows = rows;
matrix::columns = columns;
Matrix = (int **) malloc (rows * sizeof(int *));
Matrix[0] = (int *) malloc((rows*columns)*sizeof(int));
for (int i = 1; rows > i; i++) {
Matrix[i] = Matrix[i-1]+columns;
}
};
int **Matrix;
int rows, columns;
void matrixFill();
void read();
void transpose();
};
void matrix::transpose () {
/*Writes over current matrix with Row->Column flipped matrix*/
matrix tmpMatrix(columns-1, rows-1);
for(int i = 1; i < rows; i++) {
for(int j = 1; j < columns; j++)
tmpMatrix.Matrix[j][i] = matrix::Matrix[i][j];
}
free(Matrix[0]);
matrix::Matrix = tmpMatrix.Matrix; //Releasing memory
rows = tmpMatrix.rows;
columns = tmpMatrix.columns;
}
void matrix::matrixFill() {
/*Returns called self but every value is filled row by row incramenting
This mainly exists for testing and has a non-zero possibility of being removed*/
int count = 0;
for (int i = 1; i < rows; i++) {
for (int j = 1; j < columns; j++) {
Matrix[i][j] = count++;
}
}
};
void matrix::read () {
/*stdout all values of self, seperated by commas*/
std::cout << rows-1 << "x" << columns-1 << std::endl;
for(int i = 1; i < rows; i++) {
for(int j = 1; j < columns; j++) {
std::cout << matrix::Matrix[i][j] << ", ";
}
std::cout << std::endl;
}
};