Compilermeldungen versucht, zu beheben

This commit is contained in:
silvana884 2025-11-16 18:46:46 +01:00
parent a767bc6cd8
commit ad9a44673c
2 changed files with 37 additions and 30 deletions

View File

@ -6,42 +6,48 @@
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
int size = rows * cols;
MatrixType *speicherbereich;
speicherbereich = (MatrixType *)calloc(size, sizeof(MatrixType) * size);
Matrix matrix = {rows, cols, &speicherbereich};
Matrix matrix;
matrix.rows = rows;
matrix.cols = cols;
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
for (int i = 0; i < (rows * cols); i++)
matrix.buffer[i] = 0;
return matrix;
}
void clearMatrix(Matrix *matrix)
{
int rows = rows(*matrix);
int cols = cols(*matrix);
for(int i = 0; i < rows; i++)
int rowsM = matrix->rows;
int colsM = matrix->cols;
for(int i = 0; i < rowsM; i++)
{
for(int j = 0; j < cols; j++)
for(int j = 0; j < colsM; j++)
{
setMatrixAt(0, matrix, i, j);
matrix->buffer[i * colsM + j] = 0;
}
}
}
void setMatrixAt(Matrix matrix, unsigned int rowIdx, unsigned int colIdx, MatrixType value)
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
matrix->data[rowIdx * matrix->cols + colIdx] = value;
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
return matrix->data[rowIdx * matrix->cols + colIdx];
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
rows1 = rows(matrix1);
rows2 = rows(matrix2);
cols1 = cols(matrix1);
cols2 = cols(matrix2);
int rows1 = rows(matrix1);
int rows2 = rows(matrix2);
int cols1 = cols(matrix1);
int cols2 = cols(matrix2);
if((rows1 == 1 || rows2 == 1) && cols1 == cols2) //Broadcasting
{
if(rows1 == 1) //Wenn die erste Matrix der Vektor ist
@ -69,20 +75,20 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return addition;
}
else{
return 0;
return createMatrix(0,0);
}
}
Matrix broadcasting(const Matrix vektor, const Matrix matrix)
{
int rows = rows(matrix);
int cols = cols(matrix);
Matrix result = createMatrix(rows, cols);
int rowsM = rows(matrix);
int colsM = cols(matrix);
Matrix result = createMatrix(rowsM, colsM);
MatrixType wert;
MatrixType koordinate;
for(int i = 0; i < rows; i++)
for(int i = 0; i < rowsM; i++)
{
for(int j = 0; j < cols; j++){
for(int j = 0; j < colsM; j++){
wert = getMatrixAt(matrix, i, j);
koordinate = getMatrixAt(vektor, j, 0);
setMatrixAt((koordinate + wert), result, i, j);
@ -118,6 +124,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
setMatrixAt(add, result, i, k);
add = 0;
}
}
return result;
}
return createMatrix(0,0);
@ -125,10 +132,10 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
int rows(const Matrix matrix)
{
return matrix->rows;
return matrix.rows;
}
int cols(const Matrix matrix)
{
return matrix->cols;
return matrix.cols;
}

View File

@ -9,7 +9,7 @@ typedef float MatrixType;
typedef struct{
unsigned int rows;
unsigned int cols;
MatrixType data[]; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder)
MatrixType *buffer; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder)
} Matrix;
@ -18,7 +18,7 @@ void clearMatrix(Matrix *matrix);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix broadcasintg(const Matrix matrix1, const Matrix matrix2);
Matrix broadcasting(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
int rows(const Matrix matrix);
int cols(const Matrix matrix);