Answer to Question #172341 in C++ for Sehr Chowdhury

Question #172341
  1. Write a program that simulates rolling a 6-sided die until you get 3 odd numbers in a row.


Sample run

Let’s roll some dice!

You rolled a 3

You rolled a 5

You rolled a 4

You rolled a 1

You rolled a 6

You rolled a 1

You rolled a 4

You rolled a 2

You rolled a 3

You rolled a 5

Three in a row in 10 rolls.



1
Expert's answer
2021-03-16T18:53:31-0400
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


int main() {
    int n_rolls = 0;
    int n_odd = 0;
    int dice;


    srand(time(0));
    cout << "Let's roll some dice!" << endl;

    while (true) {
        dice = rand() % 6 + 1;
        cout << "You rolled a " << dice << endl;
        n_rolls++;
        if (dice %2) {
            if (++n_odd == 3) {
                break;
            }
        }
        else {
            n_odd = 0;
        }
    }
    cout << "Three in a row in " << n_rolls << " rols." << endl;

}

Fix


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS