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

  • Context: C/C++ 
  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    C++ Counting Dice
Click For Summary
SUMMARY

This discussion focuses on a C++ program that simulates rolling two dice and counts the occurrences of sums equaling six and seven. The program prompts the user for the number of rolls and utilizes a while loop to ensure valid input. It generates a histogram displaying the frequency of each possible sum from 2 to 12, with specific emphasis on the counts for sixes and sevens. The code provided demonstrates the use of random number generation and basic control structures in C++.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of random number generation in C++
  • Basic control structures: loops and conditionals
  • Familiarity with input/output operations in C++
NEXT STEPS
  • Explore C++ random number generation techniques using srand and rand
  • Learn about C++ data structures to store and analyze roll results
  • Investigate advanced statistical analysis of dice rolls
  • Study C++ graphical libraries for visualizing histogram data
USEFUL FOR

Students learning C++, game developers interested in probability simulations, and programmers looking to enhance their skills in random number generation and data visualization.

carl123
Messages
55
Reaction score
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

count << "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;
}
count << endl << "Roll " << (i + 1) << " is "
<< rollTotal << " (" << die1
<< "+" << die2 << ")";
}

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

return 0;
}
 
Technology news on Phys.org
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/.)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
17
Views
10K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K