Skip to content
Snippets Groups Projects
Commit c5c39b87 authored by dario.genga's avatar dario.genga
Browse files

Add matrix equals, get and set

Also added some matrix destroy to the tests to free the memory.
parent 18a49919
No related branches found
No related tags found
No related merge requests found
......@@ -5,9 +5,6 @@
#include <stdio.h>
#include <stdlib.h>
// #include <math.h>
// #include <time.h>
// #include <stdbool.h>
#include "matrix.h"
int main() {
......@@ -37,6 +34,24 @@ int main() {
matrix_extract_submatrix(&sub, transposed, 1, 4, 1, 3);
printf("Sub matrix :\n");
matrix_print(sub);
// Equal
if (matrix_is_equal(mat, cloned)) {
printf("The following matrix are equals :\n");
} else {
printf("The following matrix are not equals :\n");
}
printf("Matrix 1\n");
matrix_print(mat);
printf("Matrix 2\n");
matrix_print(cloned);
// Get & set
int32_t element;
matrix_get(&element, mat, 0, 0);
printf("First element of the first matrix : %d\n", element);
matrix_set(mat, 0, 0, 42);
printf("The first element of the first matrix has been changed :\n");
matrix_print(mat);
// Free the memory
matrix_destroy(&mat);
matrix_destroy(&cloned);
......
......@@ -140,3 +140,39 @@ error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t m0, i
return ok;
}
bool matrix_is_equal(matrix mat1, matrix mat2) {
if (mat1.m != mat2.m || mat1.n != mat2.n) {
return false;
}
// Parse each values of the matrix
for (int i = 0; i < mat1.m; i++) {
for (int k = 0; k < mat1.n; k++) {
if (mat1.data[i][k] != mat2.data[i][k]) {
return false;
}
}
}
return true;
}
error_code matrix_get(int32_t *elem, const matrix mat, int32_t ix, int32_t iy) {
if (ix < 0 || iy < 0 || iy >= mat.m || ix >= mat.n) {
return err;
}
*elem = mat.data[iy][ix];
return ok;
}
error_code matrix_set(matrix mat, int32_t ix, int32_t iy, int32_t elem) {
if (ix < 0 || iy < 0 || ix > mat.m || iy > mat.n) {
return err;
}
mat.data[iy][ix] = elem;
return ok;
}
......@@ -4,7 +4,6 @@
*/
#include "matrix.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
......@@ -24,8 +23,11 @@ int main() {
// Starting the tests...
printf("Starting the tests...\n");
assert(matrix_alloc(&mat, m, n) == ok);
assert(matrix_destroy(&mat) == ok);
assert(matrix_init(&mat, m, n, val) == ok);
assert(matrix_destroy(&mat) == ok);
assert(matrix_init(&mat, -1, n, val) == err);
assert(matrix_destroy(&mat) == ok);
assert(matrix_init_from_array(&mat, m, n, data, m*n) == ok);
assert(matrix_clone(&cloned, mat) == ok);
assert(matrix_transpose(&transposed, mat) == ok);
......@@ -37,8 +39,11 @@ int main() {
assert(matrix_set(mat, 0, 0, set_elem) == ok);
assert(mat.data[0][0] == set_elem);
assert(matrix_destroy(&mat) == ok);
matrix_destroy(&cloned);
matrix_destroy(&transposed);
matrix_destroy(&sub);
printf("The tests are completed and were successful !");
printf("The tests are completed and were successful !\n");
return EXIT_SUCCESS;
}
tests 0 → 100755
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment