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

AI Thread Summary
The discussion focuses on a C++ program that simulates rolling two dice and calculates the frequency of each possible sum from 2 to 12. The program prompts the user for the number of rolls, continuing to ask until a non-positive number is entered. It utilizes a while loop to manage user input effectively. After rolling the dice, it counts the occurrences of sums specifically for 6s and 7s, while also displaying a histogram that visually represents the frequency of each sum using asterisks. The code includes initialization of variables, random number generation for dice rolls, and structured output for both the results and the histogram.
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

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
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/.)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top