Dice rolls: Counting number of rolls that equals 6 or 7

In summary, this program calculates the number of times the sum of two randomly rolled dice equals six or seven. It asks the user for the number of rolls, then uses a for loop to roll the dice and track the number of sixes and sevens. It then prints the statistics and repeats the process if the user enters a number greater than or equal to 1.
  • #1
carl123
56
0
The following calculates the number of times the sum of two dice (randomly rolled) equals six or seven.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main(){ 
int i = 0; // Loop counter iterates numRolls times
int numRolls = 0; // User defined number of rolls
int numSixes = 0; // Tracks number of 6s found
int numSevens = 0; // Tracks number of 7s found
int die1 = 0; // Dice values
int die2 = 0; // Dice values
int rollTotal = 0; // Sum of dice values

cout << "Enter number of rolls: " << endl;
cin >> numRolls;

srand(time(0));

if (numRolls >= 1) {
// Roll dice numRoll times
for (i = 0; i < numRolls; ++i) {
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
rollTotal = die1 + die2;

// Count number of sixs and sevens
if (rollTotal == 6) {
numSixes = numSixes + 1;
}
else if (rollTotal == 7) {
numSevens = numSevens + 1;
}
cout << endl << "Roll " << (i + 1) << " is "
<< rollTotal << " (" << die1
<< "+" << die2 << ")";
}

// Print statistics on dice rolls
cout << endl << endl;
cout << "Dice roll statistics:" << endl;
cout << "6s: " << numSixes << endl;
cout << "7s: " << numSevens << endl;
}
else {
cout << "Invalid rolls. Try again." << endl;
}

return 0;
}

QUESTION

Create different versions of the program that:

1) Calculates the number of times the sum of the randomly rolled dice equals each possible value from 2 to 12.

2) Repeatedly asks the user for the number of times to roll the dice, quitting only when the user-entered number is less than 1. Hint: Use a while loop that will execute as long as numRolls is greater than 1. Be sure to initialize numRolls correctly.

3) Prints a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like * that number of times, as shown below.

Dice roll histogram:

2: ******
3: ****
4: ***
5: ********
6: *******************
7: *************
8: *************
9: **************
10: ***********
11: *****
12: ****
 
Technology news on Phys.org
  • #2
Can you show what you'e tried? It's hard to know how to help you when you merely post problems with no effort shown. Our goal is to guide, not do your work. Please add your work to each thread you've posted where you have not already done so, and you will find people will be much more likely to help. :)
 

Related to Dice rolls: Counting number of rolls that equals 6 or 7

What is the purpose of counting the number of dice rolls that result in 6 or 7?

The purpose of counting the number of dice rolls that result in 6 or 7 is to analyze the probability of getting these specific outcomes. This information can be useful for various applications, such as gambling or game design.

What is the mathematical probability of rolling a 6 or 7 on a single die?

The probability of rolling a 6 or 7 on a single die is 1/6 + 1/6 = 1/3. This is because there are 6 possible outcomes on a single die and only 2 of those outcomes (6 and 7) result in a 6 or 7.

Can the number of rolls that result in 6 or 7 be used to predict future outcomes?

No, the number of rolls that result in 6 or 7 cannot be used to predict future outcomes. Each dice roll is independent and the outcome of one roll does not affect the outcome of the next roll. Therefore, past results do not affect future outcomes.

How can the number of rolls that result in 6 or 7 be calculated for multiple dice?

To calculate the number of rolls that result in 6 or 7 for multiple dice, you can use the formula (n-1) * (n-2) + (n-1) * (n-2) + ... + (n-1) * (n-2) + (n-1) * (n-2) + (n-1) * (n-2) + (n-1) * (n-2) + (n-1) * (n-2) where n is the number of dice. For example, for 4 dice, the formula would be 3 * 3 + 3 * 3 + 3 * 3 + 3 * 3 = 36 possible combinations that result in 6 or 7.

Is there a limit to the number of dice that can be rolled in order to calculate the number of rolls that result in 6 or 7?

There is no limit to the number of dice that can be rolled in order to calculate the number of rolls that result in 6 or 7. However, as the number of dice increases, the number of possible combinations and the complexity of the calculation also increases.

Similar threads

  • Programming and Computer Science
Replies
1
Views
3K
Replies
17
Views
7K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top