Skip to content
Snippets Groups Projects
Commit 1b6ed1ef authored by florian.burgener's avatar florian.burgener
Browse files

Validation exercice 1

parent 1c00dbfb
Branches
No related tags found
No related merge requests found
......@@ -10,12 +10,20 @@
*/
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MATRIX_HEIGHT 5
#define MATRIX_WIDTH 4
#define OPERATION_REPETITION 10
/**
* @brief Initiliases the matrix.
*
* @param m
* @param n
* @return double**
*/
double **matrix_init(int m, int n) {
double **matrix = (double **)malloc(sizeof(double *) * m);
......@@ -30,6 +38,12 @@ double **matrix_init(int m, int n) {
return matrix;
}
/**
* @brief Frees the memory of the matrix.
*
* @param matrix
* @param m
*/
void matrix_destroy(double **matrix, int m) {
for (int i = 0; i < m; i += 1) {
free(matrix[i]);
......@@ -38,6 +52,13 @@ void matrix_destroy(double **matrix, int m) {
free(matrix);
}
/**
* @brief Displays the matrix.
*
* @param matrix
* @param m
* @param n
*/
void matrix_print(double **matrix, int m, int n) {
for (int i = 0; i < m; i += 1) {
for (int j = 0; j < n; j += 1) {
......@@ -48,7 +69,15 @@ void matrix_print(double **matrix, int m, int n) {
}
}
double **matrix_average(double **matrix, int m, int n) {
/**
* @brief Performs the averaging operation.
*
* @param matrix
* @param m
* @param n
* @return double**
*/
double **matrix_averaging(double **matrix, int m, int n) {
double **output = matrix_init(m, n);
for (int i = 0; i < m; i += 1) {
......@@ -61,7 +90,7 @@ double **matrix_average(double **matrix, int m, int n) {
value += matrix[i - 1][j];
value += matrix[i][j + 1];
value += matrix[i][j - 1];
value /= 5;
value /= 5;
} else {
value = matrix[i][j];
}
......@@ -74,17 +103,17 @@ double **matrix_average(double **matrix, int m, int n) {
}
int main() {
int m = 5;
int n = 4;
double **M = matrix_init(m, n);
for (int i = 0; i < 10; i += 1) {
double **N = matrix_average(M, m, n);
matrix_destroy(M, m);
M = N;
int m = MATRIX_HEIGHT;
int n = MATRIX_WIDTH;
double **matrix = matrix_init(m, n);
for (int i = 0; i < OPERATION_REPETITION; i += 1) {
double **matrix_2 = matrix_averaging(matrix, m, n);
matrix_destroy(matrix, m);
matrix = matrix_2;
}
matrix_print(M, m, n);
matrix_destroy(M, m);
matrix_print(matrix, m, n);
matrix_destroy(matrix, m);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment