From 5d5a51b8058d4799353d2b56353727a1eb8ffc32 Mon Sep 17 00:00:00 2001 From: Max Gloeckner Date: Wed, 11 Nov 2026 10:17:57 +0100 Subject: [PATCH] Matrixpart --- Startcode/main.c | 2 +- Startcode/matrix.c | 76 ++++++++++++++++++++++++++++++++++++---------- Startcode/matrix.h | 10 ++++-- 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/Startcode/main.c b/Startcode/main.c index a95cba3..49d6305 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -6,7 +6,7 @@ int main(int argc, char *argv[]) { - const unsigned int windowWidth = 800; + const unsigned int windowWidth = 800; //Fenstergröße const unsigned int windowHeight = 600; int exitCode = EXIT_FAILURE; diff --git a/Startcode/matrix.c b/Startcode/matrix.c index ad00628..ecb6f11 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -2,34 +2,78 @@ #include #include "matrix.h" -// TODO Matrix-Funktionen implementieren - +// TODO Matrix-Funktionen implementieren (done) +//Matrix Zeilen und Spaltenanzahl festlegen Matrix createMatrix(unsigned int rows, unsigned int cols) { - + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + clearMatrix(&matrix); + return matrix; } - -void clearMatrix(Matrix *matrix) +//Matrix Speicher leeren +void clearMatrix(Matrix *matrix) { - + for (int i=0; i < (*matrix).rows; i++) + { + for (int j = 0; j < (*matrix).cols; j++) + { + (*matrix).array[j][i] = 0; + } + } } - +//Matrix Werte setzen void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx < 28 && colIdx < 28) + { + matrix.array[rowIdx][colIdx] = value; + } } - +//Matrix Werte auslesen MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (rowIdx < 28 && colIdx < 28) + { + return matrix.array[rowIdx][colIdx]; + } + return 0; } - -Matrix add(const Matrix matrix1, const Matrix matrix2) +//Matrizen addieren +Matrix add(const Matrix matrix1, const Matrix matrix2) { - + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + if ((matrix1.rows != matrix2.rows) || (matrix1.cols != matrix2.cols)) //Überprüfung der Addierbarkeit zweier Matrizen + { + return result; + } + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix1.cols; j++) + { + result.array[i][j] = matrix1.array[i][j] + matrix2.array[i][j]; + } + } + return result; } - -Matrix multiply(const Matrix matrix1, const Matrix matrix2) +//Matrizen multiplizieren +Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + Matrix result = createMatrix(matrix1.rows, matrix2.cols); + if (matrix1.cols != matrix2.rows) //Überprüfung der Multiplizierbarkeit zweier Matrizen + { + return result; + } + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix2.cols; j++) + { + for (int k = 0; k < matrix1.cols; k++) + { + result.array[i][j] += (matrix1.array[i][k] * matrix2.array[k][j]); + } + } + } + return result; } \ No newline at end of file diff --git a/Startcode/matrix.h b/Startcode/matrix.h index cc640d1..78773de 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -5,8 +5,14 @@ typedef float MatrixType; -// TODO Matrixtyp definieren - +// TODO Matrixtyp definieren (done) +typedef struct Matrix +{ + int array[28][28]; //28 x 28 ist die Auflösung jedes Bildes + unsigned rows; //Zeilen + unsigned cols; //Spalten + short truenumber; //zwei Bite Länge am Ende jedes Bildes +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);