diff --git a/matrix.c b/matrix.c index ad00628..666e640 100644 --- a/matrix.c +++ b/matrix.c @@ -5,12 +5,28 @@ // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) -{ +{ + if (rows ==0 || cols ==0){ // check if dimensions are valid + Matrix empty = {.rows =0, .cols=0, .buffer =NULL}; //return empty matrix if invalid + return empty; + } + MatrixType *buffer = malloc(rows * cols* sizeof(MatrixType)); //allocate memory + if (buffer == NULL){ //check if memory allocation succeeded + Matrix empty = {.rows =0, .cols=0, .buffer =NULL}; //return empty matrix if out of memory + return empty; + } + Matrix result ={.rows = rows, .cols = cols, .buffer = buffer}; // create and return matrix + return result; + + } void clearMatrix(Matrix *matrix) -{ +{ free(matrix-> buffer); // free the buffer memory + matrix -> buffer = NULL; // set all members to NUll + matrix -> rows =0; + matrix -> cols =0; } diff --git a/matrix.h b/matrix.h index cc640d1..060774a 100644 --- a/matrix.h +++ b/matrix.h @@ -6,6 +6,11 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct{ + unsigned int rows; + unsigned int cols; + MatrixType *buffer; //pointer to dynamically allocate array +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); diff --git a/runMatrixTests b/runMatrixTests new file mode 100755 index 0000000..9fcd440 Binary files /dev/null and b/runMatrixTests differ