diff --git a/matrix.c b/matrix.c index 00eec00..90d4c6b 100644 --- a/matrix.c +++ b/matrix.c @@ -7,12 +7,42 @@ typedef struct Matrix { unsigned int xElement; unsigned int yElement; - + int ** data; } Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix m; + m.xElement = rows; + m.yElement = cols; + m.data = NULL; + + if(rows == 0 || cols == 0){ + m.xElement = m.yElement = 0; + return m; + } + + m.data = malloc(rows * sizeof *m.data); + if(!m.data){ + m.xElement = m.yElement = 0; + return m; + } + for(unsigned int i = 0; i < rows; i++){ + m.data[i] = malloc(cols * sizeof *m.data[i]); + + if(!m.data[i]){ + for(unsigned int j = 0; j < i; j++){ + free(m.data[j]); + } + free(m.data); + m.data = NULL; + m.xElement = m.yElement = 0; + return m; + } + + } + return m; } void clearMatrix(Matrix *matrix)