From 3ee900784e6f6a739cc79079f0c56bf7a706fc95 Mon Sep 17 00:00:00 2001 From: haugnerqu103436 Date: Thu, 5 Nov 2026 11:16:39 +0100 Subject: [PATCH] matrix logic --- Startcode/matrix.c | 30 +++++++++++++++++++++++++----- Startcode/matrix.h | 5 +++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index ad00628..9f1c1cf 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -6,27 +6,47 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - + MatrixType **storage = __builtin_malloc(rows * cols * sizeof(int)); + const Matrix matrix = {.rows = rows, .cols = cols, .storage = storage}; + + return matrix; } void clearMatrix(Matrix *matrix) { - + matrix->rows = 0; + matrix->cols = 0; + __builtin_free(matrix->storage); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + matrix.storage[rowIdx][colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + return matrix.storage[rowIdx][colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + { + return matrix1; + } + + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + + for (unsigned int i = 0; i < matrix1.rows; i++) + { + for (unsigned int j = 0; j < matrix1.cols; j++) + { + result.storage[i][j] = matrix1.storage[i][j] + matrix2.storage[i][j]; + } + } + + return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) diff --git a/Startcode/matrix.h b/Startcode/matrix.h index cc640d1..ed29aae 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -6,7 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct { + unsigned int rows; + unsigned int cols; + MatrixType** storage; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);