diff --git a/matrix.c b/matrix.c index ad00628..3464a5b 100644 --- a/matrix.c +++ b/matrix.c @@ -6,30 +6,67 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - + // Allocate memory for the matrix structure + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); + if (matrix.data == NULL) + { + // Handle memory allocation failure + matrix.rows = 0; + matrix.cols = 0; + } } void clearMatrix(Matrix *matrix) { - + // Free the allocated memory for the matrix data + if (matrix->data != NULL) + { + free(matrix->data); + matrix->data = NULL; + matrix->rows = 0; + matrix->cols = 0; + } } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + // Set the value at the specified row and column index + if (rowIdx < matrix.rows && colIdx < matrix.cols) + { + matrix.data[rowIdx * matrix.cols + colIdx] = value; + } } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + // Get the value at the specified row and column index + if (rowIdx < matrix.rows && colIdx < matrix.cols) + { + return matrix.data[rowIdx * matrix.cols + colIdx]; + } } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + // Add two matrices and return the result + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + { + // Handle dimension mismatch + Matrix emptyMatrix = createMatrix(0, 0); + return emptyMatrix; + } } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + // Multiply two matrices and return the result + if (matrix1.cols != matrix2.rows) + { + // Handle dimension mismatch + Matrix emptyMatrix = createMatrix(0, 0); + return emptyMatrix; + } } \ No newline at end of file diff --git a/matrix.h b/matrix.h index cc640d1..2beabc1 100644 --- a/matrix.h +++ b/matrix.h @@ -6,6 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct +{ + unsigned int rows; + unsigned int cols; + MatrixType *data; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols);