-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2-2.c
62 lines (45 loc) · 1.01 KB
/
p2-2.c
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
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#define M 2 /* 行の要素数 */
#define N 3 /* 列の要素数 */
/*行列の領域確保*/
double **dmatrix(int nr1, int nr2, int nl1, int nl2)
{
int i, nrow, ncol;
double **a;
nrow = nr2 - nr1 + 1; /* 行の数 */
ncol = nl2 - nl1 + 2; /* 列の数 */
/* 行の確保 */
if ( ( a = malloc( nrow*sizeof(double *) ) ) == NULL )
{
printf("メモリが確保できません(行列a)\n");
exit(1);
}
a = a - nr1; /* 行をずらす */
/* 列の確保 */
for( i=nr1; i<=nr2; i++) a[i] = malloc(ncol*sizeof(double));
for( i=nr1; i<=nr2; i++) a[i] = a[i]-nl1; /* 列をずらす */
return(a);
}
/* 行列の領域解放 */
void free_dmatrix(double **a, int nr1, int nr2, int nl1, int nl2)
{
int i;
/* メモリの解放 */
for(i=nr1; i<=nr2; i++) free((void *)(a[i]+nl1));
free((void *)(a+nr1));
}
int main(void)
{
double **a;
int i,j;
a = dmatrix(1,M,1,N);
for(i=1; i<=M; i++){
for(j=1; j<=N; j++){
a[i][j] = (i+j)/2.0;
printf("a[%d][%d]=%f \n",i,j,a[i][j]);
}
}
free_dmatrix(a,1,M,1,N);
return 0;
}