From d56dfd66223bf456bca8e4815c79bde1f246f984 Mon Sep 17 00:00:00 2001 From: KohlerMaxi Date: Sun, 8 Nov 2026 16:34:16 +0100 Subject: [PATCH] setMatrixAt implementiert und mit add angefangen --- Startcode/matrix.c | 57 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index f4ec7a6..a9270d9 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -4,7 +4,7 @@ #include "matrix.h" Matrix createMatrix(unsigned int rows, unsigned int cols){ -/* + /* unsigned int memory_depth = 0; memory_depth = rows * cols; //Meine Speichertiefe ist die Anzahl meiner Spalten mal die Reihen @@ -12,7 +12,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols){ memory_reservation = memory_depth * sizeof(MatrixType); //Nimmt die Speichertiefe mal die größe der einzelnen Speicherinhalte (float) Matrix.space = malloc(memory_reservation); //Reserviert den Speicher und gibt den Zeiger auf die erste Speicheradresse dem zeiger space (malloc "merkt" sich die Speichergröße an der adresse space) -*/ + */ Matrix matrix; //Matrix ist der Datentyp (im struct bestimmt). wir brauchen hier aber eine lokale variable (!NIX GLOBAL!) daher ist matrix eine Variable vom Typ Matrix. matrix.rows = rows; @@ -29,6 +29,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols){ } + void clearMatrix(Matrix *matrix){ //*matrix greift auf die einträge im Struct Matrix zu if (matrix == NULL) { //Wenn Matrix schon leer ist, mach nichts @@ -43,17 +44,29 @@ void clearMatrix(Matrix *matrix){ //*matrix greift auf die einträge im St } + void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx){ + if (rowIdx >= matrix.rows || colIdx >= matrix.cols) { //Gibt fehler bei falschen Idx werten aus / !Arraysyntax beachten, bei 5 rows ist im Array 0 bis 4 deviniert! + fprintf(stderr, "Error, the value could not be saved to the matrix...\n"); + return; + } + + unsigned int memoryspace = 0; + unsigned int columnlength = matrix.cols; + + memoryspace = (rowIdx * columnlength)+colIdx; //Erklärung in "getMatrixAt" + matrix.space[memoryspace] = value; } + MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx){ if (rowIdx >= matrix.rows || colIdx >= matrix.cols) { //Gibt fehler bei falschen Idx werten aus / !Arraysyntax beachten, bei 5 rows ist im Array 0 bis 4 deviniert! return UNDEFINED_MATRIX_VALUE; } - + unsigned int memoryspace = 0; //variable für den Eintrag an der gesuchten stelle unsigned int columnlength = matrix.cols; //maximale länge einer Zeile @@ -64,8 +77,46 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co } + Matrix add(const Matrix matrix1, const Matrix matrix2){ + //Matrix erstellen, in der die additionsmatrix gespeichert wird + unsigned int maxrows = 0; + unsigned int maxcols = 0; + + if(matrix1.rows >= matrix2.rows){maxrows = matrix1.rows;} + //Maximale Anzahlen der Reihen bestimmen + else {maxrows = matrix2.rows;} + + if(matrix1.cols >= matrix2.cols){maxcols = matrix1.cols;} + //Maximale Anzahl der Zeilen bestimmen + else {maxcols = matrix2.cols;} + + Matrix summatrix = createMatrix(maxrows, maxcols); //Neue Matrix erstellen + + + //Checken ob Matrizenen werte haben + if(matrix1.space == 0 || matrix2.space ==0){ + fprintf(stderr, "Error, addition of matrices was not possible...\n"); + summatrix.rows = 0; + summatrix.cols = 0; + } + + + //Prüfen welches Additionsverfahren genurtz werden soll + + if((matrix1.cols == 1 || matrix2.cols == 1) && matrix1.cols == matrix2.cols){ //Erkennung ob eine der Matrizen ein Vektor ist -> Broadcasting addition + + } + + else{ + for(int i = 0; i > summatrix.rows; i++){ + summatrix.space[i]= + } + } + + return summatrix; + }