diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 3d704d3..05e5042 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -9,24 +9,28 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix matrix; matrix.rows = rows; matrix.cols = cols; - matrix.data = malloc(rows * cols * sizeof(MatrixType)); - + matrix.buffer = NULL; + if (rows == 0 || cols == 0) + { + return; + } + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); return matrix; } void clearMatrix(Matrix *matrix) { - free(matrix->data); + free(matrix->buffer); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - matrix.data[rowIdx * matrix.cols + colIdx] = value; + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - MatrixType MatrixAt = matrix.data[rowIdx * matrix.cols + colIdx]; + MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx]; return MatrixAt; } @@ -48,4 +52,19 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols); + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix2.cols; j++) + { + MatrixType skalar = 0; + for (int k = 0; k < matrix1.cols; k++) + { + MatrixType val1 = getMatrixAt(matrix1, i, k); + MatrixType val2 = getMatrixAt(matrix2, k, j); + skalar += val1 * val2; + } + setMatrixAt(skalar, MatrixMult, i, j); + } + } + return MatrixMult; } \ No newline at end of file