Question

Required in C++ I'm asked to: Print 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. Below is my current code. I am not allowed to use arrays or anything too advanced as I just started the class. I would really appreciate the help as I've been stuck on it for a while now. I can only get it to print the number followed by the asterisk but it's supposed to print for number 2 - 12.

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main(){

int i = 0; // Loop counter iterates numRolls times

int numRolls; // User defined number of rolls

int numTwos;

int numThrees;

int numFours;

int numFives;

int numSixes; // Tracks number of 6s found

int numSevens; // Tracks number of 7s found

int numEights;

int numNines;

int numTens;

int numElevens;

int numTwelves;

int die1; // Dice values

int die2; // Dice values

int rollTotal;// Sum of dice values

  

  

numTwos = 0;

numThrees = 0;

numFours = 0;

numFives = 0;

numSixes = 0;

numSevens = 0;

numEights = 0;

numNines = 0;

numTens = 0;

numElevens = 0;

numTwelves = 0;

  

numRolls = 1;

  

while (numRolls >= 1 && numRolls != 0) {

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 == 2) {

numTwos = numTwos + 1;

}

else if (rollTotal == 3) {

numThrees = numThrees + 1;

}

else if (rollTotal == 4) {

numFours = numFours + 1;

}

else if (rollTotal == 5) {

numFives = numFives + 1;

}

else if (rollTotal == 6) {

numSixes = numSixes + 1;

}

else if (rollTotal == 7) {

numSevens = numSevens + 1;

}

else if (rollTotal == 8) {

numEights = numEights + 1;

}

else if (rollTotal == 9) {

numNines = numNines + 1;

}

else if (rollTotal == 10) {

numTens = numTens + 1;

}

else if (rollTotal == 11) {

numElevens = numElevens + 1;

}

else if (rollTotal == 12) {

numTwelves = numTwelves + 1;

}

cout << endl << "Roll " << (i + 1) << " is " << rollTotal << " (" << die1

<< "+" << die2 << ")";

}

// Print statistics on dice rolls

cout << endl << endl;

cout << "Dice roll statistics:" << endl;

cout << "2s: " << numTwos << endl;

cout << "3s: " << numThrees << endl;

cout << "4s: " << numFours << endl;

cout << "5s: " << numFives << endl;

cout << "6s: " << numSixes << endl;

cout << "7s: " << numSevens << endl;

cout << "8s: " << numEights << endl;

cout << "9s: " << numNines << endl;

cout << "10s: " << numTens << endl;

cout << "11s: " << numElevens << endl;

cout << "12s: " << numTwelves << endl;

  

cout << endl << endl;

  

cout << "Dice Histogram: " << endl;

  

if (numTwos >= 0) {

for (i = 1; i <= numTwos; i++) {

cout << "2: ";

cout << "*";

}

}

cout << endl;

  

}

else {

cout << "Invalid rolls. Try again." << endl;

}

cout << "Do you wish to continue rolling? [Press 1 to proceed or 0 to quit]" << endl;

cin >> numRolls;

}

return 0;

}

DiceStatisticsEnhanced LuisGurrolaArenivas Roll 79 1s 7 (5+2) Roll 80 is 7 (6+1) Dice roll statistics: 2s: 3 3s: 5 4s: 5 5s:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

    int i = 0;    // Loop counter iterates numRolls times

    int numRolls; // User defined number of rolls

    int numTwos;

    int numThrees;

    int numFours;

    int numFives;

    int numSixes;  // Tracks number of 6s found

    int numSevens; // Tracks number of 7s found

    int numEights;

    int numNines;

    int numTens;

    int numElevens;

    int numTwelves;

    int die1;      // Dice values

    int die2;      // Dice values

    int rollTotal; // Sum of dice values

    numTwos = 0;

    numThrees = 0;

    numFours = 0;

    numFives = 0;

    numSixes = 0;

    numSevens = 0;

    numEights = 0;

    numNines = 0;

    numTens = 0;

    numElevens = 0;

    numTwelves = 0;

    numRolls = 1;

    while (numRolls >= 1 && numRolls != 0)

    {

        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 == 2)

                {

                    numTwos = numTwos + 1;

                }

                else if (rollTotal == 3)

                {

                    numThrees = numThrees + 1;

                }

                else if (rollTotal == 4)

                {

                    numFours = numFours + 1;

                }

                else if (rollTotal == 5)

                {

                    numFives = numFives + 1;

                }

                else if (rollTotal == 6)

                {

                    numSixes = numSixes + 1;

                }

                else if (rollTotal == 7)

                {

                    numSevens = numSevens + 1;

                }

                else if (rollTotal == 8)

                {

                    numEights = numEights + 1;

                }

                else if (rollTotal == 9)

                {

                    numNines = numNines + 1;

                }

                else if (rollTotal == 10)

                {

                    numTens = numTens + 1;

                }

                else if (rollTotal == 11)

                {

                    numElevens = numElevens + 1;

                }

                else if (rollTotal == 12)

                {

                    numTwelves = numTwelves + 1;

                }

                cout << endl

                     << "Roll " << (i + 1) << " is " << rollTotal << " (" << die1

                     << "+" << die2 << ")";

            }

            // Print statistics on dice rolls

            cout << endl

                 << endl;

            cout << "Dice roll statistics:" << endl;

            cout << "2s: " << numTwos << endl;

            cout << "3s: " << numThrees << endl;

            cout << "4s: " << numFours << endl;

            cout << "5s: " << numFives << endl;

            cout << "6s: " << numSixes << endl;

            cout << "7s: " << numSevens << endl;

            cout << "8s: " << numEights << endl;

            cout << "9s: " << numNines << endl;

            cout << "10s: " << numTens << endl;

            cout << "11s: " << numElevens << endl;

            cout << "12s: " << numTwelves << endl;

            cout << endl

                 << endl;

            cout << "Dice Histogram: " << endl;

            if (numTwos >= 0)

            {

                cout << " 2 : ";

                for (i = 1; i <= numTwos; i++)

                    cout << "*";

                cout << endl;

            }

            if (numThrees >= 0)

            {

                cout << " 3 : ";

                for (i = 1; i <= numThrees; i++)

                    cout << "*";

                cout << endl;

            }

            if (numFours >= 0)

            {

                cout << " 4 : ";

                for (i = 1; i <= numFours; i++)

                    cout << "*";

                cout << endl;

            }

            if (numFives >= 0)

            {

                cout << " 5 : ";

                for (i = 1; i <= numFives; i++)

                    cout << "*";

                cout << endl;

            }

            if (numSixes >= 0)

            {

                cout << " 6 : ";

                for (i = 1; i <= numSixes; i++)

                    cout << "*";

                cout << endl;

            }

            if (numSevens >= 0)

            {

                cout << " 7 : ";

                for (i = 1; i <= numSevens; i++)

                    cout << "*";

                cout << endl;

            }

            if (numEights >= 0)

            {

                cout << " 8 : ";

                for (i = 1; i <= numEights; i++)

                    cout << "*";

                cout << endl;

            }

            if (numNines >= 0)

            {

                cout << " 9 : ";

                for (i = 1; i <= numNines; i++)

                    cout << "*";

                cout << endl;

            }

            if (numTens >= 0)

            {

                cout << "10 : ";

                for (i = 1; i <= numTens; i++)

                    cout << "*";

                cout << endl;

            }

            if (numElevens >= 0)

            {

                cout << "11 : ";

                for (i = 1; i <= numElevens; i++)

                    cout << "*";

                cout << endl;

            }

            if (numTwelves >= 0)

            {

                cout << "12 : ";

                for (i = 1; i <= numTwelves; i++)

                    cout << "*";

                cout << endl;

            }

        }

        else

        {

            cout << "Invalid rolls. Try again." << endl;

        }

        cout << "Do you wish to continue rolling? [Press 1 to proceed or 0 to quit]" << endl;

        cin >> numRolls;

    }

    return 0;

}

Roll 77 is 10 (4+6) Roll 78 is 4 (2+2) Roll 79 is 5 (1+4) Roll 80 is 4 (1+3) Dice roll statistics: 2s: 3 3s: 4 4s: 12 5s: 6 6

Add a comment
Know the answer?
Add Answer to:
Required in C++ I'm asked to: Print a histogram in which the total number of times...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • I'm trying to make a game of Craps in C++. This is how the teacher wants...

    I'm trying to make a game of Craps in C++. This is how the teacher wants the program to run. Player rolled: 6 + 4 = 10 The point is 10 Player rolled: 3 + 3 = 6 Player rolled: 6 + 3 = 9 Player rolled: 3 + 1 = 4 Player rolled: 3 + 4 = 7 You seven'd out and lost! Player rolled: 2 + 5 = 7 You won! Player rolled: 3 + 1 = 4...

  • A fair D10 die is rolled 100 times. What is the probability of rolling five 1s,...

    A fair D10 die is rolled 100 times. What is the probability of rolling five 1s, fifteen 2s, eight 3s, twelve 4s, thirteen 5s, seven 6s, fourteen 7s, six 8s, nine 9s, and eleven 10s? For the D10 die, what is the probability of rolling that value of chi square or less? xmin xmax O 1100 1400 1 1400 1700 0 1700 2000 5 2000 2300 6 2300 2600 9 2600 2900 24 2900 3200 23 3200 3500 15 3500...

  • In need of some help with a LCR C++ game. The code will run, but I...

    In need of some help with a LCR C++ game. The code will run, but I need it to cycle through the turns automatically so that the only input from the user is the number of players. It's also showing the winner as the person with 0 chips which is incorrect and I can't figure out why. Any help is greatly appreciated as this is already late. Game rules: Develop a program that follows the rules of Left Center Right...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • In the following i need to print the number of parameters that are passed to main...

    In the following i need to print the number of parameters that are passed to main (args) as well as the actual parameters (*argv). These parameters are all printed to an output file named stdout.log. I run my program 3 times with a shell script and pass different number of parameters each time, however, after opening stdout.log the only thing in stdout.log is the content from the last call and the 3 prior runs are not there at all. What...

  • C++ Write code that will input a number and print the number, the square of the...

    C++ Write code that will input a number and print the number, the square of the number, and the cube of the number. Continue the operation until 999 is entered. Write code that will request the length and width of a rectangle until 0 is entered for either the length or the width. Print the area and perimeter for each set of inputs. What is the output for the following loop? int number; number = 1; while (number < 11)...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT