alle Fehler behoben, alle Tests bestanden. Kann so genutzt werden

This commit is contained in:
welschph103497 committed 2026-11-16 20:18:38 +01:00
1 parent e1284e9efc
commit 0d41f5e631
1 file changed
+42 -19
+42 -19
View File
@@ -12,6 +12,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
matrix.buffer = NULL; matrix.buffer = NULL;
if (matrix.rows == 0 || matrix.cols == 0) if (matrix.rows == 0 || matrix.cols == 0)
{ {
matrix.rows = 0;
matrix.cols = 0;
return matrix; return matrix;
} }
matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
@@ -22,50 +24,71 @@ void clearMatrix(Matrix *matrix)
{ {
free(matrix->buffer); free(matrix->buffer);
matrix->buffer = NULL; matrix->buffer = NULL;
matrix->rows = 0;
matrix->cols = 0;
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; if ((rowIdx < matrix.rows) && (colIdx < matrix.cols))
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
return;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx]; if ((rowIdx < matrix.rows) || (colIdx < matrix.cols))
return MatrixAt; {
MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx];
return MatrixAt;
}
return UNDEFINED_MATRIX_VALUE;
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix MatrixAdd = createMatrix(matrix1.rows, matrix1.cols); unsigned int resRows = (matrix1.rows >= matrix2.rows) ? matrix1.rows : matrix2.rows;
for (int i = 0; i < matrix1.rows; i++) unsigned int resCols = (matrix1.cols >= matrix2.cols) ? matrix1.cols : matrix2.cols;
Matrix MatrixAdd = createMatrix(resRows, resCols);
if ((matrix1.rows == matrix2.rows || matrix1.rows == 1 || matrix2.rows == 1) && (matrix1.cols == matrix2.cols || (matrix1.cols == 1 || matrix2.cols == 1)) && matrix1.rows > 0 && matrix1.cols > 0 && matrix2.rows > 0 && matrix2.cols > 0)
{ {
for (int j = 0; j < matrix1.cols; j++) for (int i = 0; i < resRows; i++)
{ {
MatrixType val1 = getMatrixAt(matrix1, i, j); for (int j = 0; j < resCols; j++)
MatrixType val2 = getMatrixAt(matrix2, i, j); {
setMatrixAt(val1 + val2, MatrixAdd, i, j); MatrixType val1 = getMatrixAt(matrix1, i % matrix1.rows, j % matrix1.cols);
MatrixType val2 = getMatrixAt(matrix2, i % matrix2.rows, j % matrix2.cols);
setMatrixAt(val1 + val2, MatrixAdd, i, j);
}
} }
return MatrixAdd;
} }
return MatrixAdd; return createMatrix(0, 0);
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols); Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++) if (matrix1.cols == matrix2.rows)
{ {
for (int j = 0; j < matrix2.cols; j++) for (int i = 0; i < matrix1.rows; i++)
{ {
MatrixType skalar = 0; for (int j = 0; j < matrix2.cols; j++)
for (int k = 0; k < matrix1.cols; k++)
{ {
MatrixType val1 = getMatrixAt(matrix1, i, k); MatrixType skalar = 0;
MatrixType val2 = getMatrixAt(matrix2, k, j); for (int k = 0; k < matrix1.cols; k++)
skalar += val1 * val2; {
MatrixType val1 = getMatrixAt(matrix1, i, k);
MatrixType val2 = getMatrixAt(matrix2, k, j);
skalar += val1 * val2;
}
setMatrixAt(skalar, MatrixMult, i, j);
} }
setMatrixAt(skalar, MatrixMult, i, j);
} }
return MatrixMult;
} }
return MatrixMult; return createMatrix(0, 0);
} }