this lab confused me. Been stuck for hours Write a complete C++ program that wil
ID: 3787998 • Letter: T
Question
this lab confused me. Been stuck for hours
Write a complete C++ program that will simulate the throwing of 2 dice: The program will ask the user to choose a number in the range of 2-12 (possible values for 2 dice). The program will then simulate rolling the 2 dice until the user's number is found and keep track of how many throws it took to get that number. In order to get a more accurate number of throws, the experiment will be repeated a number of times. So, the user must also enter how many times they want to run the experiment. Specifically, Ask the user for the number that they would like to roll. Use a loop to make sure that the user's response is in the 2-12 range. Give an error message if an invalid number is entered, and then repeat the input until a valid value is entered. Ask the user how many times they would like to run the roll counting experiment. Use a loop to make sure that the user's response is at least 1. Give an error message if an invalid number is entered, and then repeat the input until a valid value is entered. To run the experiment: get 2 random dice rolls and add their values together in a loop, count how many rolls it takes to get the user's target value Repeat the count rolling experiment, keeping track of the total number of rolls, until the number of repetitions value has been reached. Write out the number of rolls for each time the experiment is run, and the average number of rolls that it took to find the user's number, (see the examples below for a suggested format). Comments: Block at beginning of file must contain: name, lab section, program number.Explanation / Answer
Hi buddy, you didn't mention any examples. Please find the below C++ implementation. Comments have been added for your better understanding
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
// your code goes here
cout<<"Enter the number that you would like to roll ";
int choice = -1;
//Reading the number until a valid input is entered
while(true){
cin >> choice;
if(choice>=2&&choice<=12){
break;
}
cout << "An invalid number is entered. Please enter again ";
}
int count = 0;
cout << "How many times you would like to run the roll counting experiment? ";
//Reading the experiment count until a valid input is entered.
while(true){
cin >> count;
if(count>=1){
break;
}
cout<<"An invalid input is entered. Please enter again ";
}
double avg ;
double total = 0;
int MAX_COUNT = count;
//Initializing for random number;
srand((unsigned)time(0));
//This loop runs the experiment a number of times
while(count-->0){
int r;
int rolls = 0;
int sum = -1,target = choice;
//This loop runs as long the sum of numbers on the dice is not equal to user's choice
while(target!=sum){
sum = ((rand()%6)+1) + ((rand()%6)+1);
rolls++;
}
//Calculating the total rolls required
total = total+rolls;
cout <<"Rolled : "<<rolls<<" times ";
}
//Calculating the average
avg = total/MAX_COUNT;
cout <<"Average number of rolls required is "<<avg<<" ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.