473,779 Members | 1,873 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 9146
Studlyami
464 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. void randomize(int *diceA, int *diceB)
  2. {
  3. srand((unsigned)+time(NULL));
  4. *diceA = rand() %6+1;
  5. srand((unsigned)+time(NULL));
  6. *diceB = rand() %6+1;
  7. }
  8.  
you only need to do the srand once.

Expand|Select|Wrap|Line Numbers
  1. int verify_1(int diceA, int diceB)
  2. {
  3. int roll;
  4. if(diceA==diceB)
  5. roll++;
  6. return roll;
  7. }
  8.  
here you initiate roll. every time you call this function roll gets reset. You are also not initializing roll to 0. int roll = 0; if not when you do roll++ you will get a messed up value.
Sep 2 '07 #31
d0ugg
41 New Member
Okay, thanks.
It looks like this now

Expand|Select|Wrap|Line Numbers
  1. int verify_1(int diceA, int diceB)
  2. {
  3.     int roll=0;
  4.     if(diceA==diceB)
  5.     roll++;
  6.     return roll;
  7. }
  8.  
But how do I initialize it from the different number everytime?
Example(Roll=1, roll=2,roll=3.. etc)

~doug
Sep 2 '07 #32
Studlyami
464 Recognized Expert Contributor
sorry i didn't make myself clear. You only want to initialize roll once, but what is happening is every time you call that function it will initialize roll. you want a function like that you will need to pass in roll by reference.

Expand|Select|Wrap|Line Numbers
  1. void verify_1(int diceA, int diceB, int *roll)
  2. {
  3.     if(diceA==diceB)
  4.     *roll++;
  5. }
  6.  
then in main before you do the for loop initialize roll to zero

int roll = 0;
Sep 2 '07 #33
d0ugg
41 New Member
Do you mean

Expand|Select|Wrap|Line Numbers
  1. int verify_1(int diceA, int diceB, float *roll)
  2. {
  3.     if(diceA==diceB)
  4.     *roll++;
  5. return *roll;
  6. }
  7.  
??
Because the compiler say that it has to return a value...

~doug
Sep 2 '07 #34
Studlyami
464 Recognized Expert Contributor
I played with your code for a bit and it was giving me problems so i decided to make it simple and put it in main. Here is the basic program, then you can try to put it into separate functions as you see fit.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     srand( (unsigned)time( NULL ) ) ;
  8.  
  9.     int diceA = 0; //will get dicea value
  10.     int diceB = 0; // will get diceb value
  11.     int DoubleTotal = 0; //keeps track each time we get a dobule
  12.  
  13.     for(int i = 0; i<1000000; i++)
  14.     {
  15.         diceA = rand()%5 +1; //value 1-6
  16.         diceB = rand()%5 +1; //value 1-6
  17.         if(diceA==diceB)
  18.         {   
  19.             DoubleTotal++;
  20.         }
  21.     }
  22.     cout<<"The dice were equal " << DoubleTotal <<" Times\n";
  23.  
  24.     return 0;
  25. }
  26.  
Sep 2 '07 #35
d0ugg
41 New Member
Thanks for the support,
but it is suppose to give the percentage of times that it happened.. But anyways, thanks!
Sep 2 '07 #36
Studlyami
464 Recognized Expert Contributor
Your Welcome. The percentage is just a simple math element at the end. You were really close at one point, you only needed to change like 2 lines, but then you split them up into functions and got confused again. I hope you can follow the logic and now that you have a working main try to split it up into the functions that you wanted.
Sep 2 '07 #37
JosAH
11,448 Recognized Expert MVP
Thanks for the support,
but it is suppose to give the percentage of times that it happened.. But anyways, thanks!
Sure, that's step 2; do you get an acceptible number for the number of times the
dice rolls were equal? e.g. total = 1,000,000, equals = 166,667.

kind regards,

Jos
Sep 2 '07 #38
d0ugg
41 New Member
Hello!

Thank you so much for the help everyone..

Have a good Sunday! ;)

~doug
Sep 2 '07 #39

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

Similar topics

5
2993
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
1410
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
2480
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
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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
10302
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
10136
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
10071
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
8958
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
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
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
3631
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.