Added error handling
This commit is contained in:
1 parent
031341d455
commit
0e8c2fe05d
1 file changed
+4
@@ -41,6 +41,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
newmatrix.cols = matrix1.cols;
|
newmatrix.cols = matrix1.cols;
|
||||||
newmatrix.rows = matrix1.rows;
|
newmatrix.rows = matrix1.rows;
|
||||||
newmatrix.buffer = malloc(matrix1.rows * matrix1.cols * sizeof(MatrixType));
|
newmatrix.buffer = malloc(matrix1.rows * matrix1.cols * sizeof(MatrixType));
|
||||||
|
if(newmatrix.buffer == NULL) return newmatrix;
|
||||||
//element addition
|
//element addition
|
||||||
for(int i = 0; i < matrix1.rows; i++){
|
for(int i = 0; i < matrix1.rows; i++){
|
||||||
for(int j = 0; j < matrix1.cols; j++){
|
for(int j = 0; j < matrix1.cols; j++){
|
||||||
@@ -53,6 +54,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
newmatrix.cols = matrix1.cols;
|
newmatrix.cols = matrix1.cols;
|
||||||
newmatrix.rows = matrix1.rows;
|
newmatrix.rows = matrix1.rows;
|
||||||
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
|
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
|
||||||
|
if(newmatrix.buffer == NULL) return newmatrix;
|
||||||
for(int i = 0; i < matrix1.rows; i++){
|
for(int i = 0; i < matrix1.rows; i++){
|
||||||
for(int j = 0; j < matrix1.cols; j++){
|
for(int j = 0; j < matrix1.cols; j++){
|
||||||
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols);
|
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix1.buffer+i*matrix1.cols+j) + *(matrix2.buffer+i*matrix2.cols);
|
||||||
@@ -64,6 +66,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
newmatrix.cols = matrix2.cols;
|
newmatrix.cols = matrix2.cols;
|
||||||
newmatrix.rows = matrix2.rows;
|
newmatrix.rows = matrix2.rows;
|
||||||
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
|
newmatrix.buffer = malloc(newmatrix.cols * newmatrix.rows * sizeof(MatrixType));
|
||||||
|
if(newmatrix.buffer == NULL) return newmatrix;
|
||||||
for(int i = 0; i < matrix1.rows; i++){
|
for(int i = 0; i < matrix1.rows; i++){
|
||||||
for(int j = 0; j < matrix2.cols; j++){
|
for(int j = 0; j < matrix2.cols; j++){
|
||||||
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix2.buffer+i*matrix2.cols+j) + *(matrix1.buffer+i*matrix1.cols);
|
*(newmatrix.buffer+i*newmatrix.cols+j) = *(matrix2.buffer+i*matrix2.cols+j) + *(matrix1.buffer+i*matrix1.cols);
|
||||||
@@ -78,6 +81,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
Matrix newmatrix = {.buffer = NULL, .cols = 0, .rows = 0};
|
Matrix newmatrix = {.buffer = NULL, .cols = 0, .rows = 0};
|
||||||
if(matrix1.cols == matrix2.rows){
|
if(matrix1.cols == matrix2.rows){
|
||||||
newmatrix.buffer = malloc(matrix1.rows * matrix2.cols * sizeof(MatrixType));
|
newmatrix.buffer = malloc(matrix1.rows * matrix2.cols * sizeof(MatrixType));
|
||||||
|
if(newmatrix.buffer == NULL) return newmatrix;
|
||||||
newmatrix.rows = matrix1.rows;
|
newmatrix.rows = matrix1.rows;
|
||||||
newmatrix.cols = matrix2.cols;
|
newmatrix.cols = matrix2.cols;
|
||||||
for(int i = 0; i < newmatrix.rows; i++){
|
for(int i = 0; i < newmatrix.rows; i++){
|
||||||
|
|||||||
Reference in new issue
Block a user