Member Avatar for Member #418472

I am trying to write this program that requires the following for C++:

Write a c++ program to simulate the roll of a die. In principle, each of six possible outcomes is equally likely.
-The program will prompt the usesr to enter the # of times the die is to be thrown. That # of random #'s in the range 1-6 will be generated and the frequency of occurance of each is to be recorded.
-The program will produce the following output:
--A statement of the number of times the die was thrown
--A well designed and formatted table with the following column headings:
Value Number of Occurrences Percent of Occurrences

The output will go down from the headings ^ and be centered.
Test data: 2 different number of throws: 1000 and 5000

SO, i can pretty much do everything, but whats keeping me stuck is the part of how the frequency of occurrence of each is to be recorded and making the table. Can anyone help, alot or even just a little?

Dani AI

Generated

was on the right track. Below is a compact, practical C++11 example that fills in the missing pieces: robust input checking, a modern random generator (better than rand()), counting, percent calculation, and a simple way to center and align the table. Use unsigned long long for large trial counts and validate the user input to avoid divide-by-zero.

#include <iostream>
#include <array>
#include <random>
#include <iomanip>
#include <string>

int main() {
    std::cout << "Enter number of throws: ";
    unsigned long long trials;
    if (!(std::cin >> trials) || trials == 0) return 1;

    std::array<unsigned long long,6> counts{};
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(1,6);

    for (unsigned long long i = 0; i < trials; ++i)
        ++counts[dist(gen) - 1];

    const int totalWidth = 60;
    std::string header = "Value    Number of Occurrences    Percent of Occurrences";
    int pad = (totalWidth - static_cast<int>(header.size())) / 2;
    if (pad > 0) std::cout << std::string(pad, ' ');
    std::cout << header << "\n\n";

    for (int v = 1; v <= 6; ++v) {
        double pct = counts[v-1] * 100.0 / trials;
        std::cout << std::setw(6) << v
                  << std::setw(22) << counts[v-1]
                  << std::setw(17) << std::fixed << std::setprecision(2) << pct << "%\n";
    }
    return 0;
}

Use <random> (std::mt19937 + std::uniform_int_distribution) for evenly distributed integers; see the reference for details: C++ random. Formatting helpers like std::setw and std::setprecision are covered here: I/O manipulators.

Troubleshooting notes: if tests use very large trial counts, ensure the counter type can hold the maximum. On some systems std::random_device may be non-deterministic; for reproducible tests seed std::mt19937 with a fixed value.

Recommended Answers

All 2 Replies

Well you could use a simple array to count how many times each number occurs.

int nums[= = {0,0,0,0,0,0};
nums[roll()-1] += 1;

You get the idea, thats assuming roll() returns a number between 1&6

Member Avatar for Member #418472

true, thanks. ill try it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.