Please write in c 1 Get from the user the number of times th

Please write in c++

(1) Get from the user the number of times they want to roll a pair of dice.

(2) Generate pseudorandom numbers to represent a roll of 2 dice. Be sure to call the rand function for each die value. Each die will have 6 sides, numbered 1 to 6. A single score is the sum of these 2 dice.

(3) Now make a loop that will do this the amount of times the user asked for. What type of loop is most appropriate? When testing, make sure you are getting proper values; the first 10 die rolls are 2 5 5 5 2 3 5 1 3 6 which means the first sum of two dice would be 7.

(4) Count how many times a score of 2 is rolled, a score of 3 is rolled, a score of 4 is rolled, and so on up to a roll of 12. Output these counts. Example table output:

(6) Now calculate and output the probability of rolling each score (the percentage of times each score was rolled). Be sure you are using floating-point division and not integer division here. Example output: (Do not format output.)

Solution

// C++ code roll 2 dice and determine percentage of the sum occured

#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main()
{
    // set the seed
    srand(time(NULL));

     // declare variables
    int die1;
    int die2;
    int sum;
    int count;
    cout << \"How many time you want to roll the dice? \";
    cin >> count;

    int times = count;

    int array[13] = {0};

    while(count --)
    {
        // roll the first die
        die1 = (rand() % 6 ) + 1;
        // roll the second die
        die2 = (rand() % 6 ) + 1;

        sum = die1+die2;

        array[sum]++;
    }

    cout << \"# of times each score was rolled\ \";
    for (int i = 2; i < 13; ++i)
    {
        cout << i << \": \" << array[i] << endl;
    }

    float percentage[13];
    for (int i = 2; i < 13; ++i)
    {
        percentage[i] = (float)array[i]/times;
        percentage[i] = percentage[i]*100;
    }

    cout << \"\ Probability of rolling each possible dice\ \";
    for (int i = 2; i < 13; ++i)
    {
        cout << setprecision(2) << i << \": \" << percentage[i] << endl;
    }

    return 0;
}

/*
output:

How many time you want to roll the dice? 1000

# of times each score was rolled
2: 33
3: 47
4: 82
5: 110
6: 122
7: 192
8: 143
9: 111
10: 84
11: 59
12: 17

Probability of rolling each possible dice
2: 3.3
3: 4.7
4: 8.2
5: 11
6: 12
7: 19
8: 14
9: 11
10: 8.4
11: 5.9
12: 1.7

*/

Please write in c++ (1) Get from the user the number of times they want to roll a pair of dice. (2) Generate pseudorandom numbers to represent a roll of 2 dice.
Please write in c++ (1) Get from the user the number of times they want to roll a pair of dice. (2) Generate pseudorandom numbers to represent a roll of 2 dice.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site