0

So I'm writing a program to roll 2 dice and record how many times a specific sum comes up, show the odds and the percentage error of what the rolls provide. However anytime the odds and the actual number of the specific sum is rolled the percentage error (which should be zero), comes up as a crazy number. I believe this is because it results in (0/1) in the cout statement, but (0/1) equals zero in C++ as I have tested it. Source code is below, it is not complete and the only sum being tested is a sum of "2", i.e. both dice roll a 1. Output of code with problem

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <string>
#pragma warning(disable: 4996)
using namespace std;

int main() {
    int dice1;
    int dice2;
    int numRolls = 36;
    char again = 'Y';
    int sum;
    int numRolled[11] {0,0,0,0,0,0,0,0,0,0,0};
    srand(time(NULL));
    while (again == 'Y' || again == 'y') {
        cout << "Please enter the number of rolls you want :";
        cin >> numRolls;
        for (int i=0; i<numRolls; i++) {
            dice1 = 1 + rand()%6;
            dice2 = 1 + rand()%6;
            //cout << dice1 << " " << dice2 << endl;
            sum = dice1 + dice2;
            switch (sum) {
                case 2 :
                    numRolled[0]++;
                    break;
                case 3 :
                    numRolled[1]++;
                    break;
                case 4 :
                    numRolled[2]++;
                    break;
                case 5 :
                    numRolled[3]++;
                    break;
                case 6 :
                    numRolled[4]++;
                    break;
                case 7 :
                    numRolled[5]++;
                    break;
                case 8 :
                    numRolled[6]++;
                    break;
                case 9 :
                    numRolled[7]++;
                    break;
                case 10 :
                    numRolled[8]++;
                    break;
                case 11 :
                    numRolled[9]++;
                    break;
                case 12 :
                    numRolled[10]++;
                    break;
                default :
                    cout << "There was a problem" << endl;
            }
        }
        cout << 0.00/1.00 << endl; //This was the test of 0/1 to see what it returned
        cout << "Sum     " << "  #Rolled" << "    Odds   " << "    Percent Error   " << endl;
        cout << " 2           " << numRolled[0] << "        " << (1.00 / 36.00) * numRolls << "            " << ((numRolled[0])-(1.00/36.00)*numRolls)/((1.00/36.00)*numRolls) * 100 << endl;//This is the line that I believe is causing a problem.
        for (int j=0; j<11; j++) {
            numRolled[j] = 0;
        }
        cout << "Would you like to roll again? (Y)/(N) :";
        cin >> again;
    }
    system("pause");
    return 0;
}
stingycam
  • 1
  • 1
  • 1
    That number is written in scientific notation. It's not a big number; it's 0, followed by a decimal point, followed by fifteen zeroes, then some other digits. It's a very small number. – Silvio Mayolo Jan 12 '22 at 04:33
  • You should cast all your ints to doubles in that calculation. e.g. static_cast(numRolls). I would do that in a seperate function though and not inline with the cout statement. Also int numRolled[11] {0,0,0,0,0,0,0,0,0,0,0}; can be written as just numRolled[11]{}; – Pepijn Kramer Jan 12 '22 at 04:34
  • Now that you point that out Silvio it is probably a floating point error. Is there any way to limit how many zeros it try's to print out in the result without printf? Also Pepijn, I did do that originally as I understood an array initializes to zero however, when I ran it, it did not want to increment the array elements unless I initialized them to zero myself. – stingycam Jan 12 '22 at 04:44
  • It was definitely the floating point error, was able to fix it with two lines, ```````````` std::cout << std::fixed; std::cout << std::setprecision(2);``````````````````````` These limit the cout to only 2 decimal places, Thank you all for the help! – stingycam Jan 12 '22 at 05:10
  • This doesn't address the question, but that big switch statement can be written much more simply as `numRolled[sum - 2]++;`. – Pete Becker Jan 12 '22 at 14:05

0 Answers0