2 つのサイコロの転がりをシミュレートするプログラムを作成します。次に、2 つの値の合計を計算し、1 つの添字付きの配列に配置する必要があります。配列を印刷します。また、12 が何回現れるかを求めよ。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 13
int main()
{
int face;
int arraysize=13;
int counter[13];
int frequency[ SIZE ]= {13};
for(int i=0; i<13; i++)
counter[i] = 0;
int die1;
int die2;
srand( time( NULL ) );
for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1+die2]++;
++frequency[ face ];
}
printf("%4s%17s\n","Sum of Values","Frequency");
for(int face=2; face<arraysize;face++)
{
printf("%8d%17d\n",face,frequency[ face ]);
}
system("PAUSE");
return 0;
}
プリントスクリーン
値の合計頻度
2 36001
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
どうしたの ???