Add Matrix struct definition and implement createMatrix function
This commit is contained in:
parent
fa52898db6
commit
f3398f9ea0
20
matrix.c
20
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
5
matrix.h
5
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);
|
||||
|
||||
BIN
runMatrixTests
Executable file
BIN
runMatrixTests
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user