473,778 Members | 1,901 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
JosAH
11,448 Recognized Expert MVP
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
Did you read my reply? You should check whether or not diceA and diceB are
equal; if so you should increment a counter. The way you do it now you simply
add both scores to the running total; that is wrong.

kind regards,

Jos
Aug 31 '07 #11
kreagan
153 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
Did you read anyone's posts except sicarie? If you did, you should have known that his suggestion was solving another problem. Also, your if statement is in the wrong place. You will only print out if diceA and diceB are equal on the last iteration.
Aug 31 '07 #12
d0ugg
41 New Member
Hello,

Sorry for been very stupid.

Well, I tried to include it on the loop but still doesn't work. It compiles but when I run, it really doesn't show the right answer.
Here is my "fixed" code.

Expand|Select|Wrap|Line Numbers
  1. #include <ctime>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. const double roll = 1000000;
  6. int totals;
  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 i;
  23.     cout << "Rolling dices one million times\n";
  24.     Randomize();
  25.  
  26.     for(i=0;i<roll;i++)
  27.     {    
  28.         diceA = rolldie();
  29.         diceB = rolldie();
  30.  
  31.         for(diceA=diceB;totals++;)
  32.         {
  33.             cout << "Doubles occurred" << totals / 1000000 << "percent of the time.";
  34.         }
  35.     }
  36.  
  37.     return 0;
  38. }
  39.  
Thank you for the help,

DOug
Aug 31 '07 #13
sicarie
4,677 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1.     for(i=0;i<roll;i++)
  2.     {    
  3.         diceA = rolldie();
  4.         diceB = rolldie();
  5.  
  6.         for(diceA=diceB;totals++;)
  7.         {
  8.             cout << "Doubles occurred" << totals / 1000000 << "percent of the time.";
  9.         }
  10.     }
  11.  
Okay, why did you decide to put the 'for(diceA=dice B;totals++;) in the for loop?

That's the thing you want to happen one time, and if it is in the for loop, it will happen every time. It would seem to me you want that outside the for loop. (Though you still want to have _something_ in there to increment totals if a is equal to b.)

Also, the diceA=diceB means you are going to assign the value of B to A. You want the '==' double equals, which is the conditional. If diceA==diceB, then do whatever - you don't need a for loop, in fact, I don't think you need anything there, just the print statement.
Aug 31 '07 #14
JosAH
11,448 Recognized Expert MVP
Sorry for been very stupid.
Nobody thinks your stupid; you just don't 'speak' C or C++ yet so lets forget
about that for a second. I give you a piece of paper and a pencil. I'm going
to roll two dice 'n' times. After every roll I show your their score.

What do you have to do after I rolled those dice? Be quick because I'm going
to roll them again. Just use simple English please.

kind regards,

Jos
Aug 31 '07 #15
d0ugg
41 New Member
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?
Aug 31 '07 #16
d0ugg
41 New Member
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.
Aug 31 '07 #17
Ganon11
3,652 Recognized Expert Specialist
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.)
Aug 31 '07 #18
JosAH
11,448 Recognized Expert MVP
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:

Expand|Select|Wrap|Line Numbers
  1. me: I show you a pair x, y
  2.    you: you do something that makes sense.
  3.  
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
Aug 31 '07 #19
kreagan
153 New Member
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.

Expand|Select|Wrap|Line Numbers
  1.     for(i=0;i<roll;i++)
  2.     {    
  3.                 cout << "Entering For Loop. i =" << i;  //added
  4.         diceA = rolldie();
  5.                 cout << "DiceA is" << diceA;   //added debug line
  6.         diceB = rolldie();
  7.                 cout <<"DiceB is" << diceB;   //added debug line
  8.         int totals = diceA + diceB;
  9.                 cout <<"totals is when dice are added together" << totals;
  10.         totals++;
  11.                 cout <<"totals is after increment" << totals;
  12.     }
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
Aug 31 '07 #20

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
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,...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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();...
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.