473,778 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble calculating percentage in C++ dice program

41 New Member
I'm writing a program that will have to roll the dice A and dice B one million times, and calculate the percentage of times that the dies will be equal.

I'm having trouble to figure out how the percentage will work.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <ctime>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. const double roll = 1000000;
  7.  
  8. int rolldie() //statement will give u 6
  9. {
  10. return (rand()%6+1);
  11. }
  12.  
  13. void Randomize() 
  14. {
  15. srand( (unsigned)time( NULL ) ) ;
  16. }
  17.  
  18. int main()
  19. {
  20.     int diceA;
  21.     int diceB;
  22.     int totals;
  23.     int i;
  24.     cout << "Rolling dices one million times\n";
  25.     Randomize();
  26.  
  27.     for(i=0;i<roll;i++)
  28.     {    
  29.         diceA = rolldie();
  30.         diceB = rolldie();
  31.         int    totals = diceA + diceB;
  32.         totals++;
  33.     }
  34.  
  35.     if(diceA==diceB)
  36.     {
  37.         cout << "Doubles occurred" << totals / 1000000 << "percent of the time.";
  38.     }
  39.  
  40.     return 0;
  41. }
  42.  
Thanks in advance,

Doug
Aug 31 '07
38 9145
d0ugg
41 New Member
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
Sep 1 '07 #21
Ganon11
3,652 Recognized Expert Specialist
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.
Sep 1 '07 #22
d0ugg
41 New Member
Good point, thank you for the quick answer..
So right now my loop code is just like that

Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<roll;i++)
  2.           {   
  3.               diceA = rolldie();
  4.                       cout << "DiceA is\n" << diceA;   //added debug line
  5.               diceB = rolldie();
  6.                       cout <<"DiceB is\n" << diceB;   //added debug line
  7.               //diceA == diceB;
  8.                      // cout <<"totals is when dice are added together" << totals;
  9.               //totals++;
  10.               //       cout <<"totals is after increment" << totals;
  11.           }
  12.  
*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:

Expand|Select|Wrap|Line Numbers
  1. int check()
  2. {
  3.     while(diceA==diceB)
  4.     return 0;
  5. }
  6.  
Does that loop work for this kind of situation?


Thanks,

~doug
Sep 1 '07 #23
Studlyami
464 Recognized Expert Contributor
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.

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i< TotalNumberOfRolls; i++)
  2. {
  3.    DiceA = Roll; //value 1-6
  4.    DiceB = Roll; //value 1-6
  5.     if(DiceA==DiceB)
  6.    {
  7.         //The Two dice are the same so incriment counter
  8.    }
  9. }
  10.  
  11. cout<<"The dice were equal" << counter<<" Times";
  12.  
Sep 1 '07 #24
kreagan
153 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. int check()
  2. {
  3.     while(diceA==diceB)
  4.     return 0;
  5. }
  6.  
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.
Sep 1 '07 #25
d0ugg
41 New Member
Hello Everyone,

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

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<roll; i++)
  2. {
  3.     diceA = rolldie(); //value 1-6
  4.     diceB = rolldie(); //value 1-6
  5.         if(diceA==diceB)
  6.         {    
  7.             i++;
  8.         }
  9. }
  10.         cout<<"The dice were equal " << i <<" Times\n";
  11.         return 0;
  12.  
- 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
Sep 2 '07 #26
Studlyami
464 Recognized Expert Contributor
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.
Sep 2 '07 #27
d0ugg
41 New Member
Okay..


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

Expand|Select|Wrap|Line Numbers
  1. for(int total=1; total<=1000000; total++)
  2. {
  3.     diceA = rolldie(); //value 1-6
  4.     diceB = rolldie(); //value 1-6
  5.         if(diceA==diceB)
  6.         {    
  7.             cout << setw(7) << "The dice were equal: " << total << endl;
  8.         }
  9. }
  10.  
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..
Sep 2 '07 #28
JosAH
11,448 Recognized Expert MVP
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
Sep 2 '07 #29
d0ugg
41 New Member
I started the code again..

Here is my code now

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. void randomize();
  7. int verify_1();
  8. void calculate_1();
  9. int rolldie();
  10.  
  11. void randomize(int *diceA, int *diceB)
  12. {
  13. srand((unsigned)+time(NULL));
  14. *diceA = rand() %6+1;
  15. srand((unsigned)+time(NULL));
  16. *diceB = rand() %6+1;
  17. }
  18.  
  19. int verify_1(int diceA, int diceB)
  20. {
  21. int roll;
  22. if(diceA==diceB)
  23. roll++;
  24. return roll;
  25. }
  26.  
  27.  
  28. void calculate_1(float *doubles)
  29. {
  30. *doubles = *doubles/1000000;
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36. int diceA;
  37. int diceB;
  38. float roll;
  39. float doubles=0;
  40.  
  41. cout << "One million dice rolls";
  42. for (roll=0; roll<1000000; roll++)
  43. {
  44. randomize(&diceA, &diceB);
  45. doubles=verify_1(diceA, diceB);
  46. calculate_1(&doubles);
  47. cout << doubles;
  48. return 0;
  49.  
  50. }
  51. }
  52.  
  53.  


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
Sep 2 '07 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2992
by: eliana82 | last post by:
I have problems calculating score percentages within groups. I have created a boat program in access where the information provided is name, team, boat and score. The first query I've done is to produce the ranking according to the score, which worked fine. The second query is about giving 100% to the highest score per boat and then taking the percentage of that score and calculate the rest of the team scores according to that score;...
4
6050
by: clairelee0322 | last post by:
Hello guys again! I am a C++ beginner and I am working on another project.. It's called Dice Lab with Loops.. unforunately I don't even know how to start the program.... Don't blame me for not pay attention in the class.. Seriously, my teacher just ask us to do the sample programs in the textbook and prepare for the finals... Everytime I ask him question... he has to open the textbook to find the answer.. I've been searching for dice...
1
1799
by: clairelee0322 | last post by:
I am a c++ beginner and i am working on a dice loop lab. This program should roll two dice (each dice is from 1 to 6) and then total them together. The user should be able to input how many times they would like to have the dice rolled. Your program should output the rolls and once the rolls are done, it should output how many of each number had been rolled. //this program will run forever and never stops expect i input the number than is...
5
8387
by: Aswanth | last post by:
I'm Using Asp.Net with C# & Working with SSRS 2005 for Generating Reports.. The Following Expression I'm using in Reports to Show the Percentage of Particular Items in REPORT.. =Round((Fields!Clicks.Value*100)/Sum(Fields!Clicks_Show.Value, "DataSet1_Get_All_1234567"),2)& "%" With this Expression I'm Getting Reports Percentage(Total) 100%(NO Problem).. But for Some Reports it is Coming >100 or < 100 Total Percentage (ie 105% or...
4
4849
by: zoeb | last post by:
Hi. I have a form which the user enters 2 years worth of data into (one record per year). The aim, is to populate the table this form is based on with 3 more years worth of data (i.e. creating 3 new records), based on a percentage increase on the previous year. This form is based directly on a table called tblBudgetShareProj. So far I have the following code but I am COMPLETELY new to VB and I'm very aware of how incomplete it is. I have a...
4
1409
lee123
by: lee123 | last post by:
hello it's me again; i am having a problem with calculating some totals in my project. I have fields on my form that need to be calculated to get a percentage in the end I wish i could show you what my form looks like but don't know how to. so i will kinda show you this way: there are 11 textboxes going across the form with lables textboxnames are as followed date,...
1
2479
by: zufie | last post by:
I have used Sum(Abs()) to convert my neg. (-) values to pos. (+) values how can I obtain the correct percentage. For example, here is my expression from my query trying to calculate the correct percentage, in this case, 2/2+3 = .4*100 = 40%. Expr18: (((Abs(Sum()))/ (Abs(Sum()))+ (Abs(Sum()))))
4
3118
by: sureshl | last post by:
function cal() { var f = document.form1; var regExp_Count = new RegExp("^+$"); f.price1.value = parseFloat(f.baseprice.value*(f.percen.value/100)).toFixed(0); } cal() functions , will calculate as such in that formula n display in the price1 text using the text property onblur,
9
2001
Steel546
by: Steel546 | last post by:
This program is used to calculate GPA. I'm having trouble actually getting an output. Alright, I KNOW that I don't have an output statement, but I don't know where to put it... heh. Netbeans keeps telling me it's wrong. So, here's my two classes. package KyleTaylorGPA; public class GPA { private double theGPA; private int gradePointsSum;
0
9470
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8957
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.