menu
bartleby
search
close search
Hit Return to see all results

Create a new file in C++ and save it as lab12_XYZ.cpp Consider rolling two six-sided dice. While the result of each throw is independent of all rolls that have come before, we can still calculate the odds of rolling any given number between 2 and 12. This program will calculate those odds by generating a large number (100,000) of random values (i.e. "rolling the dice") and storing the results in an array. When all values have been rolled, we will display the results (counts and odds to 2 decimal places) to the user.  x   x   2   3   4   5   6   7   8   9   10  11  12     //value for sum of 2 dice rolls +---+---+---+---+---+---+---+---+---+---+--- |   |   |   |   |   |   |   |   |   |   |   |   |   |    //counters for number of times value is rolled +---+---+---+---+---+---+---+---+---+---+---+---+---+   0   1   2   3   4   5   6   7   8   9  10  11  12     //array indexIn Lab 5, sample code was provided for generating a random number. In this lab, I've provided a header file for you to include, getRandomLib.h, which lets you get all kinds of random things.We need some functions:void displayMessage();  //hello, goodbye, etc.int rollDice();  //returns a number between 2 and 12 (by rolling two dice and summing the result)void displayResults(int[]);  //displays results in the arrayHINT: Make sure to initialize counters before starting loopHINT: To calculate odds: odds = count * 100 / NUM_ROLLS;Header comments must be presentPrototypes must be present if functions are usedHello and goodbye messages must be shownArray(s) must be used for implementationResults should be formatted neatly (2 decimal places for odds, decimals lined up, etc.)Use comments and good style practicesMake sure to #include "getRandomLib.h". Download the attached header file and place in same location as your program.SAMPLE OUTPUT: Welcome! This program will calculate the odds of rolling 2 - 12 with two 6-sided dice. Number of  2s rolled:          270  ( 2.70%) Number of  3s rolled:          556  ( 5.56%) Number of  4s rolled:          814  ( 8.14%) Number of  5s rolled:         1136  (11.36%) Number of  6s rolled:         1379  (13.79%) Number of  7s rolled:         1705  (17.05%) Number of  8s rolled:         1397  (13.97%) Number of  9s rolled:         1140  (11.40%) Number of 10s rolled:          818  ( 8.18%) Number of 11s rolled:          532  ( 5.32%) Number of 12s rolled:          253  ( 2.53%) Exiting program. Goodbye!Attached Files: getRandomLib.h (3.468 KB):#ifndef GET_RANDOM_LIB_H#define GET_RANDOM_LIB_H#include <cstdlib>#include <random>#include <ctime>using namespace std;const int MIN_UPPER = 65;const int MAX_UPPER = 90;const int MIN_LOWER = 97;const int MAX_LOWER = 122;const int MIN_DIGIT = 48;const int MAX_DIGIT = 57;const int MIN_CHAR = 33;const int MAX_CHAR = 126;//unsigned seed = time(0);int getRandomInt(int min, int max) {//seed random enginestatic default_random_engine gen((unsigned int)time(0));// if max < min, then swapif (min > max) {int temp = min;min = max;max = temp;}//set up random generatoruniform_int_distribution<int> dis(min, max); //range//need to throw away the first call, as it is always max...static int firstCall = dis(gen);return dis(gen);} //end getRandomInt()float getRandomFraction() {//seed random enginestatic unsigned seed = time(0);return rand() / static_cast<float>(RAND_MAX);} //end getRandomFraction()float getRandomFloat(float min, float max) {if (min > max) {float temp = min;min = max;max = temp;}return (getRandomFraction() * (max - min)) + min;} //end getRandomFloat()double getRandomDouble(double min, double max) {if (min > max) {double temp = min;min = max;max = temp;}return (static_cast<double>(getRandomFraction()) * (max - min)) + min;} //end getRandomDouble()bool getRandomBool() {if (getRandomInt(0,1) == 0)return false;elsereturn true;} //end getRandomBoolchar getRandomUpper() {return static_cast<char>(getRandomInt(MIN_UPPER, MAX_UPPER));} //end getRandomUpper()char getRandomUpper(char min, char max) { return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));} //end getRandomUpper()char getRandomLower() {return static_cast<char>(getRandomInt(MIN_LOWER, MAX_LOWER));} //end getRandomLower()char getRandomLower(char min, char max) {return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));} //end getRandomLower()char getRandomAlpha() {if (getRandomBool())return getRandomUpper();elsereturn getRandomLower();} //end getRandomAlpha()char getRandomDigit() {return static_cast<char>(getRandomInt(MIN_DIGIT, MAX_DIGIT));} //end getRandomDigit()char getRandomDigit(char min, char max) {return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));} //end getRandomDigit()char getRandomChar() {return static_cast<char>(getRandomInt(MIN_CHAR, MAX_CHAR));} //end getRandomChar()#endif

Question

Create a new file in C++ and save it as lab12_XYZ.cpp 

  1. Consider rolling two six-sided dice. While the result of each throw is independent of all rolls that have come before, we can still calculate the odds of rolling any given number between 2 and 12. This program will calculate those odds by generating a large number (100,000) of random values (i.e. "rolling the dice") and storing the results in an array. When all values have been rolled, we will display the results (counts and odds to 2 decimal places) to the user.
  x   x   2   3   4   5   6   7   8   9   10  11  12     //value for sum of 2 dice rolls +---+---+---+---+---+---+---+---+---+---+--- |   |   |   |   |   |   |   |   |   |   |   |   |   |    //counters for number of times value is rolled +---+---+---+---+---+---+---+---+---+---+---+---+---+   0   1   2   3   4   5   6   7   8   9  10  11  12     //array index

In Lab 5, sample code was provided for generating a random number. In this lab, I've provided a header file for you to include, getRandomLib.h, which lets you get all kinds of random things.

We need some functions:
  • void displayMessage();  //hello, goodbye, etc.
  • int rollDice();  //returns a number between 2 and 12 (by rolling two dice and summing the result)
  • void displayResults(int[]);  //displays results in the array

HINT: Make sure to initialize counters before starting loop

HINT: To calculate odds: odds = count * 100 / NUM_ROLLS;

  • Header comments must be present
  • Prototypes must be present if functions are used
  • Hello and goodbye messages must be shown
  • Array(s) must be used for implementation
  • Results should be formatted neatly (2 decimal places for odds, decimals lined up, etc.)
  • Use comments and good style practices

Make sure to #include "getRandomLib.h". Download the attached header file and place in same location as your program.

SAMPLE OUTPUT: Welcome! This program will calculate the odds of rolling 2 - 12 with two 6-sided dice.
Number of  2s rolled:          270  ( 2.70%) Number of  3s rolled:          556  ( 5.56%) Number of  4s rolled:          814  ( 8.14%) Number of  5s rolled:         1136  (11.36%) Number of  6s rolled:         1379  (13.79%) Number of  7s rolled:         1705  (17.05%) Number of  8s rolled:         1397  (13.97%) Number of  9s rolled:         1140  (11.40%) Number of 10s rolled:          818  ( 8.18%) Number of 11s rolled:          532  ( 5.32%) Number of 12s rolled:          253  ( 2.53%)
Exiting program. Goodbye!
Attached Files:
  •  getRandomLib.h (3.468 KB):

#ifndef GET_RANDOM_LIB_H
#define GET_RANDOM_LIB_H

#include <cstdlib>
#include <random>
#include <ctime>
using namespace std;

const int MIN_UPPER = 65;
const int MAX_UPPER = 90;
const int MIN_LOWER = 97;
const int MAX_LOWER = 122;
const int MIN_DIGIT = 48;
const int MAX_DIGIT = 57;
const int MIN_CHAR = 33;
const int MAX_CHAR = 126;

//unsigned seed = time(0);

int getRandomInt(int min, int max) {
//seed random engine
static default_random_engine gen((unsigned int)time(0));

// if max < min, then swap
if (min > max) {
int temp = min;
min = max;
max = temp;
}

//set up random generator
uniform_int_distribution<int> dis(min, max); //range

//need to throw away the first call, as it is always max...
static int firstCall = dis(gen);

return dis(gen);
} //end getRandomInt()

float getRandomFraction() {
//seed random engine
static unsigned seed = time(0);

return rand() / static_cast<float>(RAND_MAX);
} //end getRandomFraction()

float getRandomFloat(float min, float max) {

if (min > max) {
float temp = min;
min = max;
max = temp;
}

return (getRandomFraction() * (max - min)) + min;
} //end getRandomFloat()

double getRandomDouble(double min, double max) {

if (min > max) {
double temp = min;
min = max;
max = temp;
}

return (static_cast<double>(getRandomFraction()) * (max - min)) + min;
} //end getRandomDouble()

bool getRandomBool() {
if (getRandomInt(0,1) == 0)
return false;
else
return true;
} //end getRandomBool

char getRandomUpper() {
return static_cast<char>(getRandomInt(MIN_UPPER, MAX_UPPER));
} //end getRandomUpper()

char getRandomUpper(char min, char max) {
return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));
} //end getRandomUpper()

char getRandomLower() {
return static_cast<char>(getRandomInt(MIN_LOWER, MAX_LOWER));
} //end getRandomLower()

char getRandomLower(char min, char max) {
return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));
} //end getRandomLower()

char getRandomAlpha() {
if (getRandomBool())
return getRandomUpper();
else
return getRandomLower();
} //end getRandomAlpha()

char getRandomDigit() {
return static_cast<char>(getRandomInt(MIN_DIGIT, MAX_DIGIT));
} //end getRandomDigit()

char getRandomDigit(char min, char max) {
return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));
} //end getRandomDigit()

char getRandomChar() {
return static_cast<char>(getRandomInt(MIN_CHAR, MAX_CHAR));
} //end getRandomChar()

#endif

 

 

 
check_circleAnswer
Step 1

Program Instructions:

  • Declare the header files, constants, and prototypes.
  • In main() call functions displayMessage() to display hello and goodbye message.
  • Call rollDice() to get a number between 2 to 12.
  • Call displayResults() to calculate and print the odds.
  • In function displayMessage(), print the passed message.
  • In function rollDice(), use getRandomInt() to get a number between 1 to 6 twice and add them to give a summation of two dice.
  • In function displayResults() use counters and for loop to count the 2s, 3s, etc., and display the count and odds.
  • Use iomap to set the precision.
Step 2

Program:

// This program calculates the odds of getting 2-12 by rolling two dices

//header files

#include<iostream>

#include <iomanip>

#include "getRandomLib.h"

// constant declarations

const int NUM_ROLLS = 100000;

const string HELLO = "Welcome! This program will calculate the odds of rolling 2 - 12 with two 6-sided dice.";

const string GOODBYE = "Exiting program. Goodbye!";

// prototypes

void displayMessage(string msg);  //hello, goodbye, etc.

int rollDice();  //returns a number between 2 and 12 (by rolling two dice and summing the result)

void displayResults(int[]);  //displays results in the array

// main function

int main(){

     displayMessage(HELLO);

     int random_dice_no[NUM_ROLLS];

     for(int i = 0; i< NUM_ROLLS;i++){

          random_dice_no[i] = rollDice();

     }

     displayResults(random_dice_no);

     displayMessage(GOODBYE);

}

void displayMessage(string msg){

     cout<<msg<<endl;

}

int rollDice(){

     // rolling dice once

     int dice1 = getRandomInt(1,6);

     // rolling dice twice

     int dice2 = getRandomInt(1,6);

     // summing the result)

     return dice1 + dice2;

}

 

void displayResults(int result[]){

     int count2=0,count3=0,count4=0,count5=0,count6=0,count7=0,count8=0,count9=0,count10=0,count11=0,count12=0;

     for(int i = 0; i< NUM_ROLLS;i++){

          if(result[i]==2){

              count2++;

          }else if(result[i]==3){

              count3++;

          }else if(result[i]==4){

              count4++;

          }else if(result[i]==5){

              count5++;

          }else if(result[i]==6){

              count6++;

          }else if(result[i]==7){

              count7++;

          }else if(result[i]==8){

              count8++;

    ...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Prove that the following languages are not regular: (b) L = {anblak : k ≠ n + l}. (c) L = {anblak : ...

A: In the given question, multiple parts are there and we will solve only first three parts as no parti...

question_answer

Q: Design a program that prompts user enters user name and password. Until user entered user name as "F...

A: Note:The programming language is not mentioned in the question. So, it is programmed in Java.Step by...

question_answer

Q: write a program to enter two sides a and b of a right triangle from keyboard calculate its area and ...

A: Given below is the C++ program to enter the two sides of a right triangle and then calculate its are...

question_answer

Q: How can I create a decoder and a multiplexer in a circuit? The decoder has a 3 bit select input and ...

A: A decoder is a circuit which has n inputs and 2n outputs. So if there are 3 inputs to a decoder it w...

question_answer

Q: Construct the XOR operator using only AND, OR, and NOT gates.

A: XOR gate:An XOR gate is referred to an Exclusive OR gate. The output of an XOR gate is “1” only when...

question_answer

Q: Programming in C language.

A: In order to implement the question mentions a method called getChoice() information about which has ...

question_answer

Q: Give an implementation of the macroinstruction searchleft (a, qi, qj), which indicates that the mach...

A: Turing machine uses “searchleft” for an implementation of the macroinstructions. If the machine read...

question_answer

Q: Is the following distributive law valid or invalid? Prove your answer. x XOR (y + z) = (x XOR y) + ...

A: Answer:Here, the truth table is used to get the answer for each side of the question.First take LHS,

question_answer

Q: Eliminate useless productions from S → a |aA| B|C, A → aB|λ, B → Aa, C → cCD, D → ddd|Cd.

A: Identifying non-generating variables:

Sorry about that. What wasn’t helpful?