473,884 Members | 2,313 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 #1
38 9160
sicarie
4,677 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1.  
  2.     for(i=0;i<roll;i++)
  3.     {    
  4.         diceA = rolldie();
  5.         diceB = rolldie();
  6.         int    totals = diceA + diceB;
  7.         totals++;
  8.     }
  9.  
  10.  
Okay, let's walk through this for two arbitrary dice rolls.

for (i = 0; 0 < 1000000; 0++)
diceA = 3 //made this up
diceB = 4 // made this up too
totals = 3 + 4 // so totals = 7
totals++ // so totals = 8

for (i =1; 1< 1000000; 1++)
diceA = 1
diceB = 5
totals = 1+5 // totals = 6
totals++ // totals = 7

Do you see the issue here? Not only does totals get wiped out every iteration through the loop, but you also don't get the right total amount because of the increment.

I think you want it to be

totals += diceA + diceB

and no totals++. ::Edit:: and you might want to move the 'int totals' out of the loop. It will probably be seen as a re-declaration of the variable each time, in which case you'll get undefined behavior, and who knows what you will get.

I also changed the title to be a bit more descriptive - draw a few more people in, if they have something to comment on.
Aug 31 '07 #2
JosAH
11,448 Recognized Expert MVP
Expand|Select|Wrap|Line Numbers
  1.     for(i=0;i<roll;i++)
  2.     {    
  3.         diceA = rolldie();
  4.         diceB = rolldie();
  5.         int    totals = diceA + diceB;
  6.         totals++;
  7.     }
  8.  
Shouldn't you check *inside* that loop whether or not diceA equals diceB?
If they're equal you should increment a counter ('totals' mayhap?)

When the loop is finished you have rolled the dice 'roll' times and 'totals'
times they were equal.

kind regards,

Jos
Aug 31 '07 #3
d0ugg
41 New Member
Thank you so much,
That was very fast,

I will try that right now and let you guys know what happened.

Doug
Aug 31 '07 #4
sicarie
4,677 Recognized Expert Moderator Specialist
Shouldn't you check *inside* that loop whether or not diceA equals diceB?
If they're equal you should increment a counter ('totals' mayhap?)

When the loop is finished you have rolled the dice 'roll' times and 'totals'
times they were equal.

kind regards,

Jos
Does the OP even need the check to see if they are the same? Why does the OP care if they are the same? They should be dealt with the same way, right?
Aug 31 '07 #5
kreagan
153 New Member
Does the OP even need the check to see if they are the same? Why does the OP care if they are the same? They should be dealt with the same way, right?
The object of the assignment was to find the probability of doubles thrown. Your suggestion is for the average number thrown.
Aug 31 '07 #6
sicarie
4,677 Recognized Expert Moderator Specialist
The object of the assignment was to find the probability of doubles thrown. Your suggestion is for the average number thrown.
Ah, good catch.
Aug 31 '07 #7
JosAH
11,448 Recognized Expert MVP
Ah, good catch.
You should try a different brand of coffee ;-)

kind regards,

Jos (<--- Italian 'Illy' espresso)
Aug 31 '07 #8
sicarie
4,677 Recognized Expert Moderator Specialist
You should try a different brand of coffee ;-)

kind regards,

Jos (<--- Italian 'Illy' espresso)
Yeah, I'm used to my wife's coffee. She's a nurse, and when people like that work 12 hour shifts, they tend to make it very very strong. Then I come to work and I'm drinking 3 cups and just starting to feel awake... (Hence the attempt at cutting back)
Aug 31 '07 #9
d0ugg
41 New Member
So,
I did what you told me

Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<roll;i++)
  2.     {    
  3.         diceA = rolldie();
  4.         diceB = rolldie();
  5.         totals += diceA + diceB;
  6.     }
  7.  
But not it seems that is running, but it's not printing anything.
And I also put the int totals out of the loop..

Any ideas why is not printing the percentage?

Thank you so much for the help,

Doug
Aug 31 '07 #10

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

Similar topics

5
2998
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
6060
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
1809
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
8398
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
4855
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
1413
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
2493
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
3125
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
2008
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
11167
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
10768
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
10868
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
10422
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
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
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
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.