Skip to content
Snippets Groups Projects
Commit e782027f authored by Boris Stefanovic's avatar Boris Stefanovic
Browse files

save

parent 90e54c6b
Branches
Tags v1.0.0
No related merge requests found
build
*.o
*.txt
......@@ -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}
......
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment