setMatrixAt implementiert und mit add angefangen

This commit is contained in:
KohlerMaxi committed 2026-11-08 16:34:16 +01:00
1 parent fe8088ec40
commit d56dfd6622
1 file changed
+53 -2
+53 -2
View File
@@ -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,11 +44,23 @@ 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!
@@ -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;
}