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...

  • 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(){...

  • 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;       ...

  • I know I'm on the right path, but don't know where to continue from here. This is a C++ assignmen...

    I know I'm on the right path, but don't know where to continue from here. This is a C++ assignment Your job is to write that will display a calendar for any given month of a given year. The user will need to type the number of the month as an integer from 1 to 12 (1 is for January, etc.), and the year as a 4-digit integer. This assignment simply requires that your program make use of more than...

  • can someone help me fix my jeopardy dice game I am having a hard time figuring...

    can someone help me fix my jeopardy dice game I am having a hard time figuring out what i am doing wrong #include #include using namespace std; //this function makes a number between 1 and 6 come out just like a dice int rollDie() { return (rand() % 6+1); } //this function asks the user with a printed statement if they want to roll the dice and gives instructions for yes and no (y/n) void askYoNs(){ cout<<"Do you want to...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

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
ADVERTISEMENT