Skip to content

Instantly share code, notes, and snippets.

@mahata
Created September 18, 2011 21:10
Show Gist options
  • Save mahata/1225554 to your computer and use it in GitHub Desktop.
Save mahata/1225554 to your computer and use it in GitHub Desktop.
C++ Primer 7.4
#include <iostream>
long double probability(unsigned numbers, unsigned picks, unsigned mega);
int main()
{
using namespace std;
// cout.setf(ios_base::fixed, ios_base::floatfield);
double total, choices, mega;
cout << "Enter the total number of choices on the game card and the number of picks allowed and the mega number:\n";
while ((cin >> total >> choices>> mega) && choices <= total)
{
cout << "Your chance to win is: ";
cout << probability(total, choices, mega) << endl; // compute the odds
cout << "Next two numbers (q to quit):";
}
cout << "bye\n";
return 0;
}
long double probability(unsigned numbers, unsigned picks, unsigned mega)
{
long double result = 1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--) {
result = result * n / p;
}
return 1 / (result * mega);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment