Add Matrix struct definition and implement createMatrix function

This commit is contained in:
Your Name 2026-05-06 05:44:01 +02:00
parent fa52898db6
commit f3398f9ea0
3 changed files with 23 additions and 2 deletions

View File

@ -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;
}

View File

@ -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);

BIN
runMatrixTests Executable file

Binary file not shown.