|
From: PETER C. <pca...@cc...> - 2004-07-06 15:54:56
|
Remarks by per are below..i attempted to set up a counter to count the number of times a die comes up and from that calculate the percentage. so far i get very small negative numbers...
am i going in the correct direction?
panos/peter
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int rolls;
cout << "how many times do you want to roll the die? "; //how many times to roll die
cin >> rolls; //user input
int faces[6] = {0}; //array for storage of faces of die.
//used later to calculate percentage
int face;
srand(time(0)); //user wants new random #'s generated each time
int face_per;
int die[6] = {1,2,3,4,5,6};
for(int i = 0; i < rolls; i++){
face = rand();
face = 1+ face%6;
cout << "the die has rolled: " << face << endl;
}
int face_1;
int face_1_occur;
faces[0] = face_1;
face_1_occur = 0;
for(int i =0; i<rolls; i++){
face_1_occur = face_1 + 1; //counter for face 1
double prob_1 = face_1_occur * 100/rolls;
cout << "the probability to roll a 1 is" << prob_1 << endl;
}
return 0;
}
1) You don't store an infinite number of dice values - no need to. Just
emit the value for each throw on the fly. Then keep 6 counters - one for
each of the 6 faces of the dice.
2) Emit the 6 counters. Percentage is just # of occurences * 100 / total
throws.
3) If the values you put into an array are in range 1-6, of course the
values stay in that range. However, normally when counting number of
times you get a one, a two etc, you store that in an array with index 0 to
5, since all arrays in C and C++ starts with index zero.
/Per W
______________ ______________ ______________ ______________
ccny.cuny.edu
|