-2

I cant count the number of times i get strange results from otherwise simple calculations. In this simple program I'm trying to calculate the chances of derangements as seen from this numberphile vid (https://www.youtube.com/watch?v=pbXg5EI5t4c), however near the end of my main I keep getting my total percentage

<< "total: " << static_cast<double>(matches / (matches + mismatch) * 100) << endl;

always resulting in a 0 regardless of what the values are. Thanks in advance and any help is appreciated.

my whole code:

#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;

void Fill(int[], int);
void Randomize(int[], int const);

int main()
{
    int  amount = 10, //size of array sample size
         runs = 1,     //how many test runs
         matches = 0,  //how many times each shuffle has 1 match
         mismatch = 0; //how many times each shuffle has no matches
    bool match;
    unsigned seed = time(0);
    srand(seed);

    int cards[10];
    Fill(cards, amount); //fills array to desired ammount

    do
    {
       cout << "runs: ";
       cin >> runs;
       for(int i = runs; i > 0; i--)
       {

            Randomize(cards, ammount);        //shuffle
            for (int i = 0; i < ammount; i++)
            {
                if (cards[i] == i)
                {
                    match = true;
                    break;
                }
                else match = 0;
            }
            if (match)
            matches++;
            else
            mismatch++;

       }
       cout << "matches: " << matches << endl 
            << "mismatches: " << mismatch << endl
            << "total: " << static_cast<double>(matches / (matches + mismatch) * 100) << endl;
    }while (runs != 0);


    return 0;
}

void Fill(int arr[], int size)
{
     for(int i = 0; i < size; i++)
     {
             arr[i] = i;
     }
}

void Randomize(int a1[], int const size)
{
     int a2[10];
     int c = 0;
     for(int i = 0; i < size; i++)
     {
         c = (rand() % (size - i));
         a2[i] = a1[c];
         a1[c] = a1[size - i - 1];   
     }
     for(int i = 0; i < size; i++){a1[i] = a2[i];}   
}
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Anon
  • 9
  • 4
    `matches / (matches + mismatch)` is an integer division, thus the result is truncated to an integer. To make the divison floating-point, you need at least one operand to be floating-point. The simplest way would be `foo / double(bar)`. – HolyBlackCat Sep 03 '17 at 21:15
  • Also, if you use `C++11` or any other version-specific C++ tag, you should always include `C++` itself for convenience. I edited this for you. – HolyBlackCat Sep 03 '17 at 21:16
  • 2
    Also, this was already answered here: [What is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) – HolyBlackCat Sep 03 '17 at 21:17
  • Sooner or later, you should really take a look at what [`std::vector`](http://en.cppreference.com/w/cpp/container/vector), [`std::iota`](http://en.cppreference.com/w/cpp/algorithm/iota), [`std::shuffle`](http://en.cppreference.com/w/cpp/algorithm/random_shuffle) and [``](http://en.cppreference.com/w/cpp/numeric/random) can offer to you. – Bob__ Sep 03 '17 at 21:48

2 Answers2

1

In this statement, the static_cast is applied after the calculation is done.

static_cast<double>(matches / (matches + mismatch) * 100)

So what the program actually does is do the calculation in int, then convert to double, and you're getting 0.0 because the result of the integer arithmetic is zero.

To fix, manually cast at least one value to double:

(1.0 * matches / (matches + mismatch) * 100)
 ^^^
(double(matches) / (matches + mismatch) * 100)
 ^^^^^^^       ^
iBug
  • 35,554
  • 7
  • 89
  • 134
-1

The integer division does that. To prevent it, cast the value either explicitly or implicitly, e.g.

<< "total: " << (100.0 * matches / (matches + mismatch)) << endl;
Walter
  • 44,150
  • 20
  • 113
  • 196