From e782027f631c35805f9cb7d267e1a1f87d298495 Mon Sep 17 00:00:00 2001 From: Boris Stefanovic <owldev@bluewin.ch> Date: Tue, 3 May 2022 15:22:52 +0200 Subject: [PATCH] save --- .gitignore | 1 + ex1/Makefile | 4 +-- ex1/ex1.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7353e7e..08fa83e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build *.o +*.txt diff --git a/ex1/Makefile b/ex1/Makefile index b08d3eb..def2451 100644 --- a/ex1/Makefile +++ b/ex1/Makefile @@ -10,8 +10,8 @@ CC := gcc CFLAGS := -std=c11 -Wall -Wextra -pedantic CFLAGS_DEBUG := ${CFLAGS} -fsanitize=address -fsanitize=leak -g -DDEBUG -LDEXTRA := -LDEXTRA_DEBUG := +LDEXTRA := -lm +LDEXTRA_DEBUG := -lm LDFLAGS := ${CFLAGS} ${LDEXTRA} LDFLAGS_DEBUG := ${CFLAGS_DEBUG} ${LDEXTRA_DEBUG} diff --git a/ex1/ex1.c b/ex1/ex1.c index 68c42f8..124112e 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -1,6 +1,81 @@ +#include <math.h> #include <stdio.h> #include <stdlib.h> +#define ROWS 5 +#define COLS 4 +#define ITERATIONS 10 + + +double** alloc(const int m, const int n) { + double** a = malloc(m * sizeof(double*)); + for (int i = 0; i < n; ++i) { + a[i] = malloc(n * sizeof(double)); + } + return a; +} + +void destroy(double** a, const int m) { + for (int i = 0; i < m; ++i) { + free(a[i]); + } + free(a); +} + +void init(double** a, const int m, const int n) { + for (int y = 0; y < m; ++y) { + for (int x = 0; x < n; ++x) { + a[y][x] = sqrt(y + x); + } + } +} + +void mean(double** a, const int m, const int n, double** r) { + for (int y = 0; y < m; ++y) { + for (int x = 0; x < n; ++x) { + if ( + (y == 0) | + (y == (m - 1)) | + (x == 0) | + (x == (n - 1)) + ) { + r[y][x] = a[y][x]; + } else { + r[y][x] = ( + a[y][x + 1] + + a[y][x - 1] + + a[y + 1][x] + + a[y - 1][x] + + a[y][x] + ) / 5.0; + } + } + } +} + +void print(double** a, const int m, const int n) { + for (int y = 0; y < m; ++y) { + for (int x = 0; x < n; ++x) { + printf("%lf", a[y][x]); + } + printf("\n"); + } +} + int main() { + double** m = alloc(ROWS, COLS); + double** n = alloc(ROWS, COLS); + init(m, ROWS, COLS); + double** swap; + for (int i = 0; i < ITERATIONS; ++i) { + mean(m, ROWS, COLS, n); + swap = m; + m = n; + n = swap; + } + print(m, ROWS, COLS); + destroy(m, ROWS); + destroy(n, ROWS); + return EXIT_SUCCESS; } -- GitLab