Trouble calculating percentage in C++ dice program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d0ugg
    New Member
    • Jan 2007
    • 41

    #16
    Oh okay,
    But my now my question is..
    I don't know how to add in the for loop if (diceA == diceB = totals) and than increment.. Is there anywhere that I can look for that?

    Comment

    • d0ugg
      New Member
      • Jan 2007
      • 41

      #17
      Oh and by the way,

      After I roll the dices I need to calculate the percentage of times that the dices will have the same value, when the dices will equal to 7 and also when the dices will each have values of 1.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #18
        As Jos suggested, think about how you might tackle this problem without a computer. A fundamental idea about programming is an algorithm - a series of steps you must take in order to solve the problem. You can think of a correct algorithm without touching a computer. Once you have this algorithm, it is easy to translate this into code.

        Suppose I want to write a program to calculate the average score on some test I give. I have all the student's results; now what do I do? Well, I would add up all the student's results and divide by the number of students, giving me the average score per student. My algorithm would be:

        1) Retrieve the scores from my students
        2) Total these scores
        3) Divide this total by the total number of students

        From this algorithm, it would be simple to write a program.

        What you need to do is to spend some time away from your program. Stop thinking in code for a bit, and instead focus on an algorithm to determine the percentage of doubles. Once you have figured out how to do this without a computer, then and only then focus on writing code for that algorithm.

        (By the way, as Jos said, no one thinks you're stupid. There's a huge difference between 'stupid' and 'inexperienced with programming. I can almost guarantee you that most, if not all, of the moderators and experts here were, at one point, as confused as you. What tells me that you are, in fact, not stupid, is that you have so far refused to stop trying to solve this problem, and that you were smart enough to ask for help when you needed it. So, from one rookie to another (because I don't have much experience either ;) ), don't feel bad when you ask for help.)

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by d0ugg
          Oh and by the way,

          After I roll the dices I need to calculate the percentage of times that the dices will have the same value, when the dices will equal to 7 and also when the dices will each have values of 1.
          You're way too fast for me; lets go slowly: I present you an arbitrary pair of dice;
          what do you do? hint: you do have a counter which you can increment. What
          do you do if I present you the pair 5,4? Do you increment that counter if you
          want that counter to represent the number of equal pairs I've shown you?
          I guess not. You do increment that counter when I present you a pair, say: 3, 3.

          So basically this is what's happening:

          Code:
          me: I show you a pair x, y
             you: you do something that makes sense.
          Your turn: what makes sense here? Note that I sneakily indented that 'pseudo'
          code because I'm gonna show you a lot of pair of dice and you have to make
          a sensible decision over and over again.

          kind regards,

          Jos

          Comment

          • kreagan
            New Member
            • Aug 2007
            • 153

            #20
            Glad to see you are still trying this problem. Like many people already said, we all had to start at the beginning. Along with the suggestions on writing psuedo code without thinking about code (which is a good practice even for experienced programmers), place couts everywhere expecially for loops and if statements. If you have a debugger, use that too.

            For example, I added new couts to your original post.

            [CODE=c]
            for(i=0;i<roll; i++)
            {
            cout << "Entering For Loop. i =" << i; //added
            diceA = rolldie();
            cout << "DiceA is" << diceA; //added debug line
            diceB = rolldie();
            cout <<"DiceB is" << diceB; //added debug line
            int totals = diceA + diceB;
            cout <<"totals is when dice are added together" << totals;
            totals++;
            cout <<"totals is after increment" << totals;
            }[/CODE]

            This can allow you to see the flow of your code. Also, it will point out parts that you expected to behaive differently. When I started, I used them heavily and could see the logic flow of the program before I was confortable with reading code.

            Good luck

            Comment

            • d0ugg
              New Member
              • Jan 2007
              • 41

              #21
              Thanks for all the support guys, I just got home now and I going to work some more tonight on the code..

              So basically I already understood what I have to do.. Here is my way..

              First declare Dice A and also Dice B, than I want to roll Dice A and also Dice B one million times. After that I will see how many times Dice A equals to Dice B, but unfortunately I don't know how to do that in the for loop yet, I tried a couple things but still get some errors..

              Thanks,

              ~doug

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #22
                Originally posted by d0ugg
                Thanks for all the support guys, I just got home now and I going to work some more tonight on the code..

                So basically I already understood what I have to do.. Here is my way..

                First declare Dice A and also Dice B, than I want to roll Dice A and also Dice B one million times. After that I will see how many times Dice A equals to Dice B, but unfortunately I don't know how to do that in the for loop yet, I tried a couple things but still get some errors..

                Thanks,

                ~doug
                You're on the right track. But you have the order wrong. Once you have rolled DiceA and DiceB 1,000,000 times, how are you going to check if they were ever equal? Once you re-roll a die, you've lost its previous number. You need to check if DiceA and DiceB are equal right after they've been rolled - inside the loop with their rolls.

                Comment

                • d0ugg
                  New Member
                  • Jan 2007
                  • 41

                  #23
                  Good point, thank you for the quick answer..
                  So right now my loop code is just like that

                  Code:
                  for(i=0;i<roll;i++)
                            {   
                                diceA = rolldie();
                                        cout << "DiceA is\n" << diceA;   //added debug line
                                diceB = rolldie();
                                        cout <<"DiceB is\n" << diceB;   //added debug line
                                //diceA == diceB;
                                       // cout <<"totals is when dice are added together" << totals;
                                //totals++;
                                //       cout <<"totals is after increment" << totals;
                            }
                  *Where I put // its because I don't know the syntax, I'm not sure yet how I will check diceA == DiceB.

                  *
                  I just had an idea, I decided to create a check function that will see if diceA and diceB is equal..

                  So now, I have also this in my code:

                  Code:
                  int check()
                  {
                  	while(diceA==diceB)
                  	return 0;
                  }
                  Does that loop work for this kind of situation?


                  Thanks,

                  ~doug

                  Comment

                  • Studlyami
                    Recognized Expert Contributor
                    • Sep 2007
                    • 464

                    #24
                    Your close. Rather than doing while DiceA==DiceB use an if statment during the for loop. You don't want to use while (DiceA==DiceB) because while is a loop and the loop will stop when the conditions are not met (I.E. DiceA is not equal to DiceB). We are not interested in how many time in a row DiceA == DiceB. We just want how many times out of a million rolls were they equal.

                    Below is the basic flow your looking for.

                    Code:
                    for(int i = 0; i< TotalNumberOfRolls; i++)
                    {
                       DiceA = Roll; //value 1-6
                       DiceB = Roll; //value 1-6
                        if(DiceA==DiceB)
                       {
                            //The Two dice are the same so incriment counter
                       }
                    }
                    
                    cout<<"The dice were equal" << counter<<" Times";

                    Comment

                    • kreagan
                      New Member
                      • Aug 2007
                      • 153

                      #25
                      Originally posted by d0ugg
                      Good point, thank you for the quick answer..
                      So right now my loop code is just like that

                      So now, I have also this in my code:

                      Code:
                      int check()
                      {
                      	while(diceA==diceB)
                      	return 0;
                      }
                      Does that loop work for this kind of situation?


                      Thanks,

                      ~doug
                      Great idea on creating the function. As for what is in the function, the loop won't work What is happening is, the while loop checks if the statement is true. If it is true, a zero will be returned. If it isn't true, nothing will be return (that's bad). Because of the return statement, you only run the while loop once. But if you remove the return statement, the program run the while loop forever or never. (Try placing the return 0 with cout << "I'm in the while loop" and you will see what I mean.) But there is another tool you can use that will only check it once. People have been hinting towards it. An IF statement.

                      When looking at an if statement, read it as

                      if ( something is true ) {
                      do X
                      }
                      else if ( something else is true ) { // if above statement is not true
                      do Y
                      }
                      else // if nothing above is true
                      do Z
                      }

                      where X, Y, and Z are just different procedures. For example returning 0 or rolling a dice. Whatever you want to put into it.

                      Comment

                      • d0ugg
                        New Member
                        • Jan 2007
                        • 41

                        #26
                        Hello Everyone,

                        Thanks for all the support one more time..
                        So right now my loop looks like this:

                        Code:
                        for(int i=0; i<roll; i++)
                        {
                        	diceA = rolldie(); //value 1-6
                        	diceB = rolldie(); //value 1-6
                        		if(diceA==diceB)
                        		{	
                        			i++;
                        		}
                        }
                        		cout<<"The dice were equal " << i <<" Times\n";
                        		return 0;
                        - The compiler does not reject my code, but it is a little bit weird because out of one million random rolls, it does not find any time that the Dice A = Dice B..

                        ~doug

                        Comment

                        • Studlyami
                          Recognized Expert Contributor
                          • Sep 2007
                          • 464

                          #27
                          a couple of problems there.
                          1.) depending on your compiler when you do the cout<<"the dice... <<i<<
                          'i' was created in the for loop and on some compliers might lose i at the end of the loop (VS 2005 won't even let it compile).

                          2.) even if 'i' retained its value you are going to have i = 1,000,000 because the loop will stop when i >= 1,000,000. At the end of every loop you increase it by one. You also increase 'i' if the dice rolls are the same. You need 2 seperate counters. one to count the rolls the other to count the doubles.

                          fix those and it works like a charm.

                          Comment

                          • d0ugg
                            New Member
                            • Jan 2007
                            • 41

                            #28
                            Okay..


                            Finally I got one step done..
                            I got the loop working correctly..
                            Here is the code

                            Code:
                            for(int total=1; total<=1000000; total++)
                            {
                            	diceA = rolldie(); //value 1-6
                            	diceB = rolldie(); //value 1-6
                            		if(diceA==diceB)
                            		{	
                            			cout << setw(7) << "The dice were equal: " << total << endl;
                            		}
                            }
                            I just realize that i don't need to increment anything, because it will already run one million times and see when they are equal..
                            But now my problem is..

                            Calculate the percentage of times that they are equal..

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #29
                              Originally posted by d0ugg
                              I just realize that i don't need to increment anything, because it will already run one million times and see when they are equal..
                              But now my problem is..

                              Calculate the percentage of times that they are equal..
                              Well, you've realized it wrong then ;-) Instead of telling the world everytime the
                              dice were equal you should increment a counter; that counter indicates the number
                              of equal rolls when the loop has finished; now you have two numbers:

                              1) 'roll' the number of rolls in total (here: 1,000,000)
                              2) 'counter' the number of rolls that were equal.

                              Now you do the simple math.

                              kind regards,

                              Jos

                              Comment

                              • d0ugg
                                New Member
                                • Jan 2007
                                • 41

                                #30
                                I started the code again..

                                Here is my code now

                                Code:
                                #include <iostream>
                                #include <ctime>
                                
                                using namespace std;
                                
                                void randomize();
                                int verify_1();
                                void calculate_1();
                                int rolldie();
                                
                                void randomize(int *diceA, int *diceB)
                                {
                                srand((unsigned)+time(NULL));
                                *diceA = rand() %6+1;
                                srand((unsigned)+time(NULL));
                                *diceB = rand() %6+1;
                                }
                                
                                int verify_1(int diceA, int diceB)
                                {
                                int roll;
                                if(diceA==diceB)
                                roll++;
                                return roll;
                                }
                                
                                
                                void calculate_1(float *doubles)
                                {
                                *doubles = *doubles/1000000;
                                }
                                
                                
                                int main()
                                {
                                int diceA;
                                int diceB;
                                float roll;
                                float doubles=0;
                                
                                cout << "One million dice rolls";
                                for (roll=0; roll<1000000; roll++)
                                {
                                randomize(&diceA, &diceB);
                                doubles=verify_1(diceA, diceB);
                                calculate_1(&doubles);
                                cout << doubles;
                                return 0;
                                
                                }
                                }


                                However when I run it shows me the percentage 0.. I don't know where I add something wrong..

                                The compiler does not give me any error, but gives me 2 warnings..

                                1 warning '=' : conversion from 'int' to 'float', possible loss of data
                                2 warning = uninitialized local variable 'roll' used



                                Thanks,

                                ~doug

                                Comment

                                Working...