Print the totals of 2 dice rolls 36000 times - C++ Data Type

C++ examples for Data Type:Array

Description

Print the totals of 2 dice rolls 36000 times

Demo Code

#include <cstdlib>
#include <iomanip>
#include <iostream>

int rollDie();/*ww w  . ja va 2  s. c o m*/

static int totalRolls = 36000;
static const int total = 13;

int main(int argc, const char *argv[]) {
  int tally[total] = {};

  srand(time(0));

  for (int i = 0; i < totalRolls; ++i) {
    ++tally[rollDie() + rollDie()];
  }

  for (int i = 2; i < total; ++i) {
    std::cout << std::setw(2) << i << ": " << tally[i] << std::endl;
  }

  return 0;
}
// roll a single die
int rollDie() {
  return rand() % 6 + 1;
}

Result


Related Tutorials