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

Add matrix allocation, initialisation and print

Also added the tests for the allocation and initiaisation.
parent 2b648f9c
No related branches found
No related tags found
No related merge requests found
/* Author : Dario GENGA /* Author : Dario GENGA
* Date : 13.10.2021 * Date : 16.11.2021
* Description : Template for a standard c file * Description : Manipulate matrix
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// #include <math.h> // #include <math.h>
// #include <time.h> // #include <time.h>
// #include <stdbool.h>
#include "matrix.h" #include "matrix.h"
int main() { int main() {
printf("Hello world!"); matrix mat;
matrix_init(&mat, 3, 4, 0);
matrix_print(mat);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
LIB=-lm LIB=-lm
CC=gcc -Wall -Wextra -g CC=gcc -Wall -Wextra -g
run_tests: tests
./$<
matrix: matrix.o main.o matrix: matrix.o main.o
gcc $^ -fsanitize=address -o $@ $(LIB) $(CC) $^ -fsanitize=address -fsanitize=leak -o $@ $(LIB)
tests: test.o matrix.o
$(CC) $^ -fsanitize=address -fsanitize=leak -o $@ $(LIB)
matrix.o: matrix.c matrix.h matrix.o: matrix.c matrix.h
$(CC) -c $< $(LIB) $(CC) -c $< $(LIB)
main.o: main.c main.o: main.c
$(CC) -c $< $(LIB) $(CC) -c $< $(LIB)
clean: clean:
rm -f *.o matrix rm -f *.o matrix tests
\ No newline at end of file \ No newline at end of file
/* Author : Dario GENGA /* Author : Dario GENGA
* Date : 15.11.2021 * Date : 16.11.2021
* Description : Manipulate an unidimensional array with dynamic memory allocation * Description : Manipulate matrix
*/ */
#include "matrix.h" #include "matrix.h"
#include <stdio.h> #include <stdio.h>
error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) {
int32_t **data = malloc(m * sizeof(int32_t *));
mat->m = m;
mat->n = n;
if (data == NULL) {
return err;
}
mat->data = data;
for (int32_t i = 0; i < m; i++) {
int32_t *row = malloc(n * sizeof(int32_t *));
if (row == NULL) {
return err;
}
mat->data[i] = row;
}
return ok;
}
error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val) {
matrix_alloc(mat, m, n);
for (int32_t i = 0; i < m; i++) {
for (int32_t k = 0; k < n; k++) {
mat->data[i][k] = val;
}
}
return ok;
}
error_code matrix_print(const matrix mat) {
for (int i = 0; i < mat.m; i++) {
printf("[");
for (int k = 0; k < mat.n; k++) {
printf(" %d ", mat.data[i][k]);
}
printf("]\n");
}
return ok;
}
void swap(int *x, int *y) void swap(int *x, int *y)
{ {
int tmp = *x; int tmp = *x;
......
/* Author : Dario GENGA /* Author : Dario GENGA
* Date : 15.11.2021 * Date : 16.11.2021
* Description : Manipulate an unidimensional array with dynamic memory allocation * Description : Manipulate matrix
*/ */
#ifndef _MATRIX_H #ifndef _MATRIX_H
#define _MATRIX_H #define _MATRIX_H
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
typedef struct _matrix {
int32_t m, n;
int32_t ** data;
} matrix;
typedef enum _error_code {
ok, err
} error_code;
error_code matrix_alloc(matrix *mat, int32_t m, int32_t n);
error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val);
error_code matrix_destroy(matrix *mat);
error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t data[], int32_t s);
error_code matrix_clone(matrix *cloned, const matrix mat);
error_code matrix_transpose(matrix *transposed, const matrix mat);
error_code matrix_print(const matrix mat);
error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t m0, int32_t m1, int32_t n0, int32_t n1);
bool matrix_is_equal(matrix mat1, matrix mat2);
error_code matrix_get(int32_t *elem, const matrix mat, int32_t ix, int32_t iy);
error_code matrix_set(matrix mat, int32_t ix, int32_t iy, int32_t elem);
void swap(int *x, int *y); void swap(int *x, int *y);
......
test.c 0 → 100644
/* Author : Dario GENGA
* Date : 16.11.2021
* Description : Manipulate matrix
*/
#include "matrix.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
int main() {
// Initialisation
matrix mat;
int32_t m = 3;
int32_t n = 4;
int32_t val = 1;
printf("Starting the tests...\n");
// Test allocation
assert(matrix_alloc(&mat, m, n) == ok);
// Test initialisation
assert(matrix_init(&mat, m, n, val) == ok);
printf("The tests are completed and were successful !");
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