|
From: Per W. <pw...@ia...> - 2004-07-06 18:12:18
|
Let's see now:
You have one array
int faces[6]
which you don't seem to use anywhere in your program - other than to
assign uninitialized variables (face_1) to index position 0:
int face_1;
faces[0] = face_1;
Maybe you wanted to write:
face_1 = faces[0];
since the goal should be that faces[0] stored the count of ones you got
from all the rolls?
Then you have one other array
int die[6] = {1,2,3,4,5,6};
that is most definitely not needed.
Maybe you somewhere in the program - in your first fir loop, where you
throw your dices - should use the first array and do:
faces[face-1]++;
to count up number on ones, twoes... while you are busy rolling the
dices.
Aftwerards, faces[0] would be number of times you got a one. faces[1]
would be the number of times you got a two etc and faces[0] + faces[1] +
... + faces[5] would be the total number of times you rolled the dice,
i.e. the same as the value rolls. 100*faces[0] / rolls would then be
number of times you got a one.
Since faces[0] .. faces[5] would then contain counted values, you never
need a second for loop in the program for building statistics. You could
however use the second loop to emit your 6 percentages: probability of
getting a one, probability of getting a two...
I think I recommended you once before to always build your programs with
the setting -Wall active. That would have generated a warning about face_1
being used without having been initialized. Always let the compiler
generate as much warnings as possible. For a biginning programmer, all
warnings are useful. If you think a warning is stupid, you should know
specifically why the compiler emitted the warning, and why the warning
isn't apropriate for your specific program. When you are ready to keep a 5
minute presentation in front of your class about a specific warning, then
you have enough knowledge to decide when to ignore warnings of that
specific type. Before that, you should treat warnings as errors, and make
sure your code compiles with zero warnings at the highest warning level
(-Wall).
/Per W
On Tue, 6 Jul 2004, PETER CAMPOS wrote:
> 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
|