config ifndef CONFIG_H define CONFIG_H define 05 define define int def

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//config.h
#ifndef CONFIG_H
#define CONFIG_H
#define t 0.05
#define h 0.1
#define N (int)((1 / t) / 2)
#define MAXH (int)(1 / h)
#endif
//2.c
#include <stdio.h>
#include "config.h"
#define NEXT ((n+1)%2)
#define THIS (n%2)
void fillArray(double *array) {
int i;
for (i = -N; i <= N; i++) {
if (i <= 0)
array[i] = 1;
else
array[i] = 0;
}
}
int main(void) {
double r = t / h;
double A[2][2*N + 1]; // [-N..N]
double *u[2] = { &A[0][0] + N, &A[1][0] + N };
int j;
int n;
fillArray(u[0]);
fillArray(u[1]);
for (n = 0; n < MAXH; n++)
for (j = -N+1; j <= N; j++) {
u[NEXT][j] = (1 - r) * u[THIS][j] + r * u[THIS][j-1];
}
for (j = -N+1; j <= N; j++) {
printf("%lf %lf\n", j * t, u[THIS][j]);
}
return 0;
}