diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 643114f..627e49a 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -11,7 +11,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) matrix.rows = rows; matrix.cols = cols; - matrix.values = malloc(rows * cols * sizeof(MatrixType)); + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); return matrix; @@ -22,27 +22,82 @@ void clearMatrix(Matrix *matrix) if(matrix == NULL){ return; } - for(int i = 0; i < values; i++){ - - free() - } + //hier reicht das zum freigeben da der gesamte buffer nur ein Speicherblock sind, nicht wie beim ImageInput, somit reich + //einmal auf den buffer im Matrix struct zu zeigen und diesen freizugeben + free(matrix->buffer); } + + //schreibt einen Wert an eine bestimmte Stelle in der Matrix void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + + //diese Prüfung muss man machen, da nicht vorhandene Werte nicht gesetzt werden können + //bei einem Row-Index von 2 bräuchte die Matrix 3 Zeilen, somit müssen die Zeilen immer eins größer sein als der Index + if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ + + return; + } + + //Formel um die richtige Position im Speicherblock zu berechnen: Index = (Zeile * Anzahl_Spalten) + Spalte + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + } + + //Liest einen Wert aus der Matrix MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + + if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ + + return UNDEFINED_MATRIX_VALUE; + } + //selbe Formel wie bei setMatrix nur dass man hier den jeweiligen Wert zurückgeben soll + return matrix.buffer[rowIdx * matrix.cols + colIdx]; + } + + Matrix add(const Matrix matrix1, const Matrix matrix2) { - -} + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + + + if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){ + + for(int i = 0; i < matrix1.rows; i++){ + for(int j = 0; j < matrix1.cols; j++){ + + MatrixType value1 = getMatrixAt(matrix1, i, j); + MatrixType value2 = getMatrixAt(matrix2, i ,j); + + setMatrixAt(value1 + value2, result, i, j); + + } + } + return result; + + + } + + + + + + result.buffer = NULL; + result.rows = 0; + result.cols = 0; + + return result; + + + + +} +//hier muss Spalte der ersten Matrix = Zeile der zweiten Matrix sein Matrix multiply(const Matrix matrix1, const Matrix matrix2) {