|
From: nuc p. <nuc...@ms...> - 2004-07-07 02:36:20
|
<html><div style='background-color:'><DIV class=RTE>
<P>per...you're very patient. thanx again. panos/peter<BR><BR></P></DIV>
<DIV></DIV>>From: Per Westermark <pw...@ia...>
<DIV></DIV>>To: PETER CAMPOS <pca...@cc...>
<DIV></DIV>>CC: dev...@li...
<DIV></DIV>>Subject: Re: [Dev-C++] die rolling problem
<DIV></DIV>>Date: Tue, 6 Jul 2004 22:57:46 +0200 (CEST)
<DIV></DIV>>
<DIV></DIV>>I don't know what you need face_1 ... face_6 for.
<DIV></DIV>>
<DIV></DIV>>int faces[6] is an array of 6 integers - same as the number of sides on a
<DIV></DIV>>dice. You start by setting each value to zero.
<DIV></DIV>>
<DIV></DIV>>Whenever you roll a dice, you get an asnwer between 1 and 6. If you get
<DIV></DIV>>the value 1, you increment faces[1-1]. If you get the dice value 2, you
<DIV></DIV>>increment faces[2-1] and so on.
<DIV></DIV>>
<DIV></DIV>>Let's assume that you do 100 rolls. When your for loop is finished, you
<DIV></DIV>>might write something like:
<DIV></DIV>> printf("Number of ones: %d.\n",faces[0]);
<DIV></DIV>> ...
<DIV></DIV>> printf("Number of sixes: is %d.\n",faces[5]);
<DIV></DIV>>
<DIV></DIV>>and get the output:
<DIV></DIV>> Number of ones: 17
<DIV></DIV>> ...
<DIV></DIV>> Number of sixes: 16
<DIV></DIV>>
<DIV></DIV>>There is no need for a second loop, other than at the end of the program
<DIV></DIV>>if you want to emit the percentage (or total count) you got of each of the
<DIV></DIV>>six sides of the dice. The same loop that throws the dice 'roll' number of
<DIV></DIV>>times will at the same time count the number of occurrences of the
<DIV></DIV>>different sides of the dice.
<DIV></DIV>>
<DIV></DIV>>/Per W
<DIV></DIV>>
<DIV></DIV>>On Tue, 6 Jul 2004, PETER CAMPOS wrote:
<DIV></DIV>>
<DIV></DIV>> > per, i follow your logic up until:
<DIV></DIV>> > "Maybe you somewhere in the program - in your first fir loop, where you
<DIV></DIV>> > >throw your dices - should use the first array and do:
<DIV></DIV>> > > faces[face-1]++;
<DIV></DIV>> > >to count up number on ones, twoes... while you are busy rolling the
<DIV></DIV>> > >dices."
<DIV></DIV>> >
<DIV></DIV>> > how would i implement the array into counting how many ties a number comes up while at the same time not incrementing the number of rolls? i understand that faces[0]=faces[1-1], for example but i don't know how to use it properly. i tried the do..while loop below but i know taht i am not using it properly.
<DIV></DIV>> > thanx peter
<DIV></DIV>> >
<DIV></DIV>> > #include <iostream>
<DIV></DIV>> > #include <ctime>
<DIV></DIV>> > #include <cstdlib>
<DIV></DIV>> > using namespace std;
<DIV></DIV>> > int main()
<DIV></DIV>> > {
<DIV></DIV>> > int faces[6] = {0};
<DIV></DIV>> > int face_1, face_2, face_3, face_4, face_5, face_6;
<DIV></DIV>> > face_1 = faces[0];
<DIV></DIV>> > face_2 = faces[1];
<DIV></DIV>> > face_3 = faces[2];
<DIV></DIV>> > face_4 = faces[3];
<DIV></DIV>> > face_5 = faces[4];
<DIV></DIV>> > face_6 = faces[5];
<DIV></DIV>> >
<DIV></DIV>> > int rolls;
<DIV></DIV>> > cout << "how many times do you want to roll the die? "; //how many times to roll die
<DIV></DIV>> > cin >> rolls;
<DIV></DIV>> >
<DIV></DIV>> > srand(time(0));
<DIV></DIV>> > int face;
<DIV></DIV>> > for(int i= 0; i < rolls; i++){
<DIV></DIV>> > do{
<DIV></DIV>> > face= rand();
<DIV></DIV>> > face= 1+ face%6;
<DIV></DIV>> > cout << "the die has rolled a:" << face << endl;
<DIV></DIV>> > }while (faces[face-1]++);
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> > }
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> > return 0;
<DIV></DIV>> >
<DIV></DIV>> > }
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> > ---------- Original Message ----------------------------------
<DIV></DIV>> > From: Per Westermark <pw...@ia...>
<DIV></DIV>> > Date: Tue, 6 Jul 2004 20:11:23 +0200 (CEST)
<DIV></DIV>> >
<DIV></DIV>> > >
<DIV></DIV>> > >
<DIV></DIV>> > >Let's see now:
<DIV></DIV>> > >
<DIV></DIV>> > >You have one array
<DIV></DIV>> > > int faces[6]
<DIV></DIV>> > >which you don't seem to use anywhere in your program - other than to
<DIV></DIV>> > >assign uninitialized variables (face_1) to index position 0:
<DIV></DIV>> > > int face_1;
<DIV></DIV>> > > faces[0] = face_1;
<DIV></DIV>> > >Maybe you wanted to write:
<DIV></DIV>> > > face_1 = faces[0];
<DIV></DIV>> > >since the goal should be that faces[0] stored the count of ones you got
<DIV></DIV>> > >from all the rolls?
<DIV></DIV>> > >
<DIV></DIV>> > >Then you have one other array
<DIV></DIV>> > > int die[6] = {1,2,3,4,5,6};
<DIV></DIV>> > >that is most definitely not needed.
<DIV></DIV>> > >
<DIV></DIV>> > >Maybe you somewhere in the program - in your first fir loop, where you
<DIV></DIV>> > >throw your dices - should use the first array and do:
<DIV></DIV>> > > faces[face-1]++;
<DIV></DIV>> > >to count up number on ones, twoes... while you are busy rolling the
<DIV></DIV>> > >dices.
<DIV></DIV>> > >
<DIV></DIV>> > >Aftwerards, faces[0] would be number of times you got a one. faces[1]
<DIV></DIV>> > >would be the number of times you got a two etc and faces[0] + faces[1] +
<DIV></DIV>> > >... + faces[5] would be the total number of times you rolled the dice,
<DIV></DIV>> > >i.e. the same as the value rolls. 100*faces[0] / rolls would then be
<DIV></DIV>> > >number of times you got a one.
<DIV></DIV>> > >
<DIV></DIV>> > >Since faces[0] .. faces[5] would then contain counted values, you never
<DIV></DIV>> > >need a second for loop in the program for building statistics. You could
<DIV></DIV>> > >however use the second loop to emit your 6 percentages: probability of
<DIV></DIV>> > >getting a one, probability of getting a two...
<DIV></DIV>> > >
<DIV></DIV>> > >I think I recommended you once before to always build your programs with
<DIV></DIV>> > >the setting -Wall active. That would have generated a warning about face_1
<DIV></DIV>> > >being used without having been initialized. Always let the compiler
<DIV></DIV>> > >generate as much warnings as possible. For a biginning programmer, all
<DIV></DIV>> > >warnings are useful. If you think a warning is stupid, you should know
<DIV></DIV>> > >specifically why the compiler emitted the warning, and why the warning
<DIV></DIV>> > >isn't apropriate for your specific program. When you are ready to keep a 5
<DIV></DIV>> > >minute presentation in front of your class about a specific warning, then
<DIV></DIV>> > >you have enough knowledge to decide when to ignore warnings of that
<DIV></DIV>> > >specific type. Before that, you should treat warnings as errors, and make
<DIV></DIV>> > >sure your code compiles with zero warnings at the highest warning level
<DIV></DIV>> > >(-Wall).
<DIV></DIV>> > >
<DIV></DIV>> > >/Per W
<DIV></DIV>> > >
<DIV></DIV>> > >On Tue, 6 Jul 2004, PETER CAMPOS wrote:
<DIV></DIV>> > >
<DIV></DIV>> > >> 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...
<DIV></DIV>> > >> am i going in the correct direction?
<DIV></DIV>> > >> panos/peter
<DIV></DIV>> > >>
<DIV></DIV>> > >> #include <iostream>
<DIV></DIV>> > >> #include <ctime>
<DIV></DIV>> > >> #include <cstdlib>
<DIV></DIV>> > >> using namespace std;
<DIV></DIV>> > >> int main()
<DIV></DIV>> > >> {
<DIV></DIV>> > >>
<DIV></DIV>> > >> int rolls;
<DIV></DIV>> > >> cout << "how many times do you want to roll the die? "; //how many times to roll die
<DIV></DIV>> > >> cin >> rolls; //user input
<DIV></DIV>> > >> int faces[6] = {0}; //array for storage of faces of die.
<DIV></DIV>> > >> //used later to calculate percentage
<DIV></DIV>> > >> int face;
<DIV></DIV>> > >> srand(time(0)); //user wants new random #'s generated each time
<DIV></DIV>> > >> int face_per;
<DIV></DIV>> > >>
<DIV></DIV>> > >> int die[6] = {1,2,3,4,5,6};
<DIV></DIV>> > >> for(int i = 0; i < rolls; i++){
<DIV></DIV>> > >> face = rand();
<DIV></DIV>> > >> face = 1+ face%6;
<DIV></DIV>> > >> cout << "the die has rolled: " << face << endl;
<DIV></DIV>> > >> }
<DIV></DIV>> > >>
<DIV></DIV>> > >> int face_1;
<DIV></DIV>> > >> int face_1_occur;
<DIV></DIV>> > >> faces[0] = face_1;
<DIV></DIV>> > >> face_1_occur = 0;
<DIV></DIV>> > >> for(int i =0; i<rolls; i++){
<DIV></DIV>> > >> face_1_occur = face_1 + 1; //counter for face 1
<DIV></DIV>> > >>
<DIV></DIV>> > >> double prob_1 = face_1_occur * 100/rolls;
<DIV></DIV>> > >> cout << "the probability to roll a 1 is" << prob_1 << endl;
<DIV></DIV>> > >> }
<DIV></DIV>> > >> return 0;
<DIV></DIV>> > >> }
<DIV></DIV>> > >>
<DIV></DIV>> > >> 1) You don't store an infinite number of dice values - no need to. Just
<DIV></DIV>> > >> emit the value for each throw on the fly. Then keep 6 counters - one for
<DIV></DIV>> > >> each of the 6 faces of the dice.
<DIV></DIV>> > >>
<DIV></DIV>> > >> 2) Emit the 6 counters. Percentage is just # of occurences * 100 / total
<DIV></DIV>> > >> throws.
<DIV></DIV>> > >>
<DIV></DIV>> > >> 3) If the values you put into an array are in range 1-6, of course the
<DIV></DIV>> > >> values stay in that range. However, normally when counting number of
<DIV></DIV>> > >> times you get a one, a two etc, you store that in an array with index 0 to
<DIV></DIV>> > >> 5, since all arrays in C and C++ starts with index zero.
<DIV></DIV>> > >>
<DIV></DIV>> > >> /Per W
<DIV></DIV>> > >
<DIV></DIV>> > >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> > ______________ ______________ ______________ ______________
<DIV></DIV>> > ccny.cuny.edu
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> > -------------------------------------------------------
<DIV></DIV>> > This SF.Net email sponsored by Black Hat Briefings & Training.
<DIV></DIV>> > Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
<DIV></DIV>> > digital self defense, top technical experts, no vendor pitches,
<DIV></DIV>> > unmatched networking opportunities. Visit www.blackhat.com
<DIV></DIV>> > _______________________________________________
<DIV></DIV>> > Dev-cpp-users mailing list
<DIV></DIV>> > Dev...@li...
<DIV></DIV>> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
<DIV></DIV>> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
<DIV></DIV>> >
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>-------------------------------------------------------
<DIV></DIV>>This SF.Net email sponsored by Black Hat Briefings & Training.
<DIV></DIV>>Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
<DIV></DIV>>digital self defense, top technical experts, no vendor pitches,
<DIV></DIV>>unmatched networking opportunities. Visit www.blackhat.com
<DIV></DIV>>_______________________________________________
<DIV></DIV>>Dev-cpp-users mailing list
<DIV></DIV>>Dev...@li...
<DIV></DIV>>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
<DIV></DIV>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
<DIV></DIV></div><br clear=all><hr> <a href="http://g.msn.com/8HMAENUS/2740??PS=47575">MSN Toolbar provides one-click access to Hotmail from any Web page FREE download!</a> </html>
|