C++ Dice rolls: Counting number of rolls that equals six or seven. ()

In summary, this program simulates rolling a pair of dice a user-defined number of times and tracks the number of times the sum of the dice equals 6 or 7. It uses a for loop to roll the dice and a series of if statements to count the number of sixes and sevens. The program also provides statistics on the number of sixes and sevens rolled.
  • #1
carl123
56
0
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: ****#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;
}
 
Technology news on Phys.org
  • #2
Please use [code]...[/code] tags around the code (the button with # symbol on the toolbar above the text area where you type your post).

What is your question? What have you tried? What did not work? (Please see http://mathhelpboards.com/rules/.)
 

What is C++ Dice rolls and why is it important?

C++ Dice rolls is a programming concept that involves simulating the rolling of dice and counting the number of times a specific outcome occurs. It is important because it allows us to analyze the probability of certain events and make informed decisions based on that information.

How do I count the number of rolls that equal six or seven in C++?

To count the number of rolls that equal six or seven in C++, you can use a loop to repeatedly generate a random number between 1 and 6 (representing the six sides of a die) and then check if the number is equal to six or seven. If it is, you can increment a counter variable. After the loop has finished, the value of the counter variable will represent the number of rolls that equal six or seven.

Can I use C++ to simulate multiple dice rolls at once?

Yes, you can use C++ to simulate multiple dice rolls at once by using a nested loop. The outer loop will represent the number of times you want to simulate the rolls, and the inner loop will generate a random number between 1 and 6 for each roll.

How can I modify my C++ program to count the number of rolls that equal a different number?

You can modify your C++ program to count the number of rolls that equal a different number by changing the condition in the if statement to check for that specific number. For example, if you want to count the number of rolls that equal three, you would change the if statement to if (num == 3).

Are there any libraries or functions in C++ that can help with simulating dice rolls?

Yes, there are libraries and functions in C++ that can help with simulating dice rolls, such as the rand() function in the cstdlib library. This function can generate a random number between 0 and RAND_MAX, which you can then manipulate to fit the range of numbers on a die (e.g. by using the modulus operator).

Similar threads

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