I am woking in C++. The following is the problem: 35.10.1 (Dice rolls), with thi
ID: 3673370 • Letter: I
Question
I am woking in C++. The following is the problem:
35.10.1 (Dice rolls), with this variation: Add code to calculate and print the number of times that the user rolls a 12 (as well as 6 and 7)
You will need to make three changes to the code:
declare a variable for the number of twelves (include the comment)
increment the variable whenever the roll total equals 12, and
display the total number of 12s rolled.
This is the given:
#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;
Explanation / Answer
Solution for Question 1:
Updated the code with by declareing variable twelve and added the value is for calculating die value..
See the below code for ..
#include <iostream>
#include <cstdlib>
#include <ctime>
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 die3 = 0; // Dice values
int rollTotal = 0; // Sum of dice values
int twelves = 0; // Tracks number of 12s found
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;
die3 = rand() % 6 + 1;
rollTotal = die1 + die2 + die3;
// Count number of sixs and sevens
if (rollTotal == 6) {
numSixes = numSixes + 1;
}
else if (rollTotal == 7) {
numSevens = numSevens + 1;
}
else if (rollTotal == 12) {
twelves = twelves + 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;
cout << "12ss: " << twelves << endl;
}
else {
cout << "Invalid rolls. Try again." << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.