From 9b993719e8e2650f1cd9aa7af25bdbc8b67c0cfb Mon Sep 17 00:00:00 2001 From: Henrik Batz Date: Thu, 5 Nov 2026 09:59:07 +0100 Subject: [PATCH] Implement matrix.c --- Startcode/matrix.c | 119 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 12 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index ad00628..e8498e1 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -2,34 +2,129 @@ #include #include "matrix.h" -// TODO Matrix-Funktionen implementieren - +// Matrix erstellen Matrix createMatrix(unsigned int rows, unsigned int cols) { - + Matrix matrix; + + matrix.rows = rows; + matrix.cols = cols; + + matrix.data = calloc(rows * cols, sizeof(MatrixType)); + + if (matrix.data == NULL) + { + matrix.rows = 0; + matrix.cols = 0; + } + + return matrix; } -void clearMatrix(Matrix *matrix) +// Matrix-Speicher freigeben +void clearMatrix(Matrix* matrix) { - + if (matrix != NULL) + { + free(matrix->data); + + matrix->data = NULL; + matrix->rows = 0; + matrix->cols = 0; + } } -void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) +// Wert an einer bestimmten Position setzen +void setMatrixAt(MatrixType value, Matrix matrix, + unsigned int rowIdx, unsigned int colIdx) { - + if (matrix.data != NULL && + rowIdx < matrix.rows && + colIdx < matrix.cols) + { + matrix.data[rowIdx * matrix.cols + colIdx] = value; + } } -MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) +// Wert an einer bestimmten Position auslesen +MatrixType getMatrixAt(const Matrix matrix, + unsigned int rowIdx, unsigned int colIdx) { - + if (matrix.data != NULL && + rowIdx < matrix.rows && + colIdx < matrix.cols) + { + return matrix.data[rowIdx * matrix.cols + colIdx]; + } + + return UNDEFINED_MATRIX_VALUE; } +// Zwei Matrizen addieren Matrix add(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.rows != matrix2.rows || + matrix1.cols != matrix2.cols || + matrix1.data == NULL || + matrix2.data == NULL) + { + return createMatrix(0, 0); + } + + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + + if (result.data == NULL) + { + return result; + } + + for (unsigned int i = 0; i < matrix1.rows; i++) + { + for (unsigned int j = 0; j < matrix1.cols; j++) + { + MatrixType value = + getMatrixAt(matrix1, i, j) + + getMatrixAt(matrix2, i, j); + + setMatrixAt(value, result, i, j); + } + } + + return result; } +// Zwei Matrizen multiplizieren Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - -} \ No newline at end of file + if (matrix1.cols != matrix2.rows || + matrix1.data == NULL || + matrix2.data == NULL) + { + return createMatrix(0, 0); + } + + Matrix result = createMatrix(matrix1.rows, matrix2.cols); + + if (result.data == NULL) + { + return result; + } + + for (unsigned int i = 0; i < matrix1.rows; i++) // nur < weil bei 0 angefangen zu zählen + { + for (unsigned int j = 0; j < matrix2.cols; j++) + { + MatrixType sum = 0; + + for (unsigned int k = 0; k < matrix1.cols; k++) + { + sum += getMatrixAt(matrix1, i, k) * + getMatrixAt(matrix2, k, j); + } + + setMatrixAt(sum, result, i, j); + } + } + + return result; +}