- Roll a six-sided die 6000 times. Then show the output frequency elements 1-6 in
tabular format. Use the following
srand( time( 0 ) ); // seed random-number generator
face = 1 + rand() % 6 ; // generate rand face from 1:6
create B[face] list to count the number of occurrence the faces 1 -6,
Show the output as
Face Frequency
1 1003
2 1004
3 999
4 980
5 1013
6 1001
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int B[7] = {0}; // B[0] doesn't used
srand(time(0)); // seed random-number generator
for (int i=0; i<6000; i++) {
int face = 1 + rand()%6; // generate rand face from 1:6
B[face]++;
}
cout << "Face Frequency" << endl;
for (int i=1; i<=6; i++) {
cout << i << " " << B[i] << endl;
}
}
Comments
Leave a comment