質問するログイン新規登録

質問編集履歴

2

済み

2019/10/13 17:15

投稿

hikaru_love_n
hikaru_love_n

スコア16

title CHANGED
File without changes
body CHANGED
@@ -1,59 +1,6 @@
1
1
  3個のサイコロを n 回振ったときに出た目の和がどのような分布になっているか, を調べるプログラム dice3.c を作る. このプログラムに必要になる下記の 2 つの関数を作成し, ファイル dice3sub.c に格納せよ.
2
2
 
3
- void dice_throw(int d[ ], int n)
4
- 1〜6 の整数の一様乱数を n 個発生し, d[0]〜d[n-1] に格納する.
5
- void dice3_distr(int n, int d1[ ], int d2[ ], int d3[ ], int c[ ])
6
- d1[0]〜d1[n-1] は 1 個目のサイコロを n 回振ったときに出た目を記録したものであり, d2[0]〜d2[n-1] は 2 個目のサイコロを n 回振ったときに出た目を記録したものであり, d3[0]〜d3[n-1] は 3 個目のサイコロを n 回振ったときに出た目を記録したものとする. この関数は, n, d1[ ], d2[ ], d3[ ] を受け取り, d1[i] と d2[i] と d3[i] の和 s が出現した回数を c[s] にカウントする.
7
- dice3sub.c からは次の dice3sub.h (および必要な標準ヘッダ) をインクルードすること.
8
- dice3sub.h
9
- #define N_MAX 1024
10
- #define D3_MIN (1+1+1)
11
- #define D3_MAX (6+6+6)
12
3
 
13
- void dice_throw(int d[ ], int n);
14
- void dice3_distr(int n, int d1[ ], int d2[ ], int d3[ ], int c[ ]);
15
-
16
- 次の dice3.c とリンクして, 動作を確認せよ.
17
- dice3.c
18
- #include <stdio.h>
19
- #include <stdlib.h>
20
- #include <assert.h>
21
- #include "dice3sub.h"
22
-
23
- int main(void)
24
- {
25
- int d1[N_MAX]; /* 1個目のサイコロの出目の系列 */
26
- int d2[N_MAX]; /* 2個目のサイコロの出目の系列 */
27
- int d3[N_MAX]; /* 3個目のサイコロの出目の系列 */
28
- int n; /* サイコロを振る回数 */
29
- int c[D3_MAX+1]; /* 目の和の出現回数 */
30
- int i;
31
-
32
- fprintf(stderr, "サイコロを振る回数 n = ");
33
- scanf("%d", &n);
34
- assert(0<n && n<=N_MAX);
35
-
36
- srand(1);
37
- for (i=D3_MIN; i<=D3_MAX; i++) {
38
- c[i] = rand();
39
- }
40
-
41
- srand(1);
42
- dice_throw(d1, n);
43
- dice_throw(d2, n);
44
- dice_throw(d3, n);
45
- dice3_distr(n, d1, d2, d3, c);
46
-
47
- for (i=D3_MIN; i<=D3_MAX; i++) {
48
- printf("%2d: %6d\n", i, c[i]);
49
- }
50
-
51
- return 0;
52
- }
53
-
54
- というプログラムの関数部分を書く、プログラムなのですが、現在
55
-
56
-
57
4
  このように書いているところです。しかしn=2で実行したところ
58
5
  サイコロを振る回数 n = 2
59
6
  3: 1481765933

1

改善

2019/10/13 17:15

投稿

hikaru_love_n
hikaru_love_n

スコア16

title CHANGED
File without changes
body CHANGED
@@ -52,36 +52,8 @@
52
52
  }
53
53
 
54
54
  というプログラムの関数部分を書く、プログラムなのですが、現在
55
- #include<stdio.h>
56
- #include<stdlib.h>
57
- #include "dice3sub.h"
58
55
 
59
- void dice_throw(int d[], int n){
60
-
61
- int i;
62
-
63
- for(i = 0; i <= n - 1; i++){
64
-
65
- d[i] = rand() % 6 + 1;
66
- }
67
- }
68
56
 
69
- void dice3_distr(int n, int d1[], int d2[], int d3[], int c[]){
70
-
71
- int i;
72
- int s;
73
-
74
- for(i = 0; i <= n - 1; i++){
75
-
76
- c[s] = 0;
77
- s = d1[i] + d2[i] + d3[i];
78
-
79
- c[s]++;
80
-
81
- s = 0;
82
- }
83
- }
84
-
85
57
  このように書いているところです。しかしn=2で実行したところ
86
58
  サイコロを振る回数 n = 2
87
59
  3: 1481765933