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

Add matrix initialization from array

parent 22a2d56b
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,16 @@
int main() {
matrix mat;
matrix_init(&mat, 3, 4, 0);
int32_t m = 3;
int32_t n = 4;
int32_t s = m * n;
int32_t val = 0;
int32_t data[12] = { 2, 1, -1, -2, 3, 1, 1, 3, 1, 4, -1, -1 };
matrix_init(&mat, m, n, val);
printf("Empty matrix %dx%d\n", m, n);
matrix_print(mat);
matrix_init_from_array(&mat, m, n, data, s);
printf("Matrix %dx%d from array\n", m, n);
matrix_print(mat);
return EXIT_SUCCESS;
......
......@@ -49,6 +49,22 @@ error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val) {
return ok;
}
error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t data[], int32_t s) {
if (m <= 0 || n <= 0 || m * n != s) {
return err;
}
matrix_alloc(mat, m, n);
for (int i = 0; i < m; i++) {
for (int k = 0; k < n; k++) {
mat->data[i][k] = data[k + (i * n)];
}
}
return ok;
}
error_code matrix_print(const matrix mat) {
for (int i = 0; i < mat.m; i++) {
printf("[");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment