fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. #include<iomanip>
  5.  
  6. using namespace std;
  7.  
  8. unsigned long long int fact(int n);
  9. unsigned long long int C(int n, int r);
  10.  
  11. int main() {
  12.  
  13. cout << setfill('-') << setw(80) << "-" << endl;
  14.  
  15. cout << "You can use this to find the probabilities for a binomial expansion." << '\n';
  16. cout << "Please check that the individual probabilities add up to 1." << '\n';
  17. cout << setfill('-') << setw(80) << "-" << endl;
  18.  
  19. int n;
  20. while (cout << "Please enter the sample size:\n", cin >> n)
  21. {
  22. double p;
  23. cout << "Enter the probability of a certain behaviour occurring:\n";
  24. cin >> p;
  25.  
  26.  
  27. long double k = 0;
  28. int i;
  29. long double j;
  30.  
  31. for (i = 0; i <= n; i++) {
  32.  
  33. j = C(n, i)*pow(p, i)*pow(1 - p, n - i);
  34.  
  35. const unsigned label_width = n < 100 ? 11 : 13;
  36. const unsigned prec = 6;
  37. const unsigned num_width = p < 1.0 ? prec+3 : prec+7;
  38. const unsigned spacer = 10;
  39.  
  40. const std::string label1 = "P(x = " + std::to_string(i) + ")";
  41. const std::string label2 = "P(x <= " + std::to_string(i) + ")";
  42.  
  43. std::cout << setfill(' ') << std::fixed << std::setprecision(prec);
  44.  
  45. cout << left << setw(label_width) << label1 << " = ";
  46. cout << right << setw(num_width) << j;
  47. cout << setw(spacer) << "";
  48. cout << left << setw(label_width) << label2 << " = ";
  49. cout << right << setw(num_width) << j + k << '\n';
  50.  
  51. k = j + k;
  52.  
  53. }
  54. }
  55.  
  56. return 0;
  57.  
  58. }
  59.  
  60. unsigned long long int fact(int n) {
  61.  
  62. unsigned long long int t;
  63. unsigned long long int answer;
  64.  
  65. answer = 1;
  66. for (t = 1; t <= n; t++) answer = answer * t;
  67.  
  68. return(answer);
  69. }
  70.  
  71. unsigned long long int C(int n, int r) {
  72.  
  73. unsigned long long int a = 1;
  74.  
  75. unsigned long long int p = n - r;
  76.  
  77. for (n; n>p; n--) { a = a*n; }
  78.  
  79. a = a / fact(r);
  80.  
  81. return a;
  82.  
  83. }
Success #stdin #stdout 0s 3464KB
stdin
15 .5
5 2.3
stdout
--------------------------------------------------------------------------------
You can use this to find the probabilities for a binomial expansion.
Please check that the individual probabilities add up to 1.
--------------------------------------------------------------------------------
Please enter the sample size:
Enter the probability of a certain behaviour occurring:
P(x = 0)    =  0.000031          P(x <= 0)   =  0.000031
P(x = 1)    =  0.000458          P(x <= 1)   =  0.000488
P(x = 2)    =  0.003204          P(x <= 2)   =  0.003693
P(x = 3)    =  0.013885          P(x <= 3)   =  0.017578
P(x = 4)    =  0.041656          P(x <= 4)   =  0.059235
P(x = 5)    =  0.091644          P(x <= 5)   =  0.150879
P(x = 6)    =  0.152740          P(x <= 6)   =  0.303619
P(x = 7)    =  0.196381          P(x <= 7)   =  0.500000
P(x = 8)    =  0.196381          P(x <= 8)   =  0.696381
P(x = 9)    =  0.152740          P(x <= 9)   =  0.849121
P(x = 10)   =  0.091644          P(x <= 10)  =  0.940765
P(x = 11)   =  0.041656          P(x <= 11)  =  0.982422
P(x = 12)   =  0.013885          P(x <= 12)  =  0.996307
P(x = 13)   =  0.003204          P(x <= 13)  =  0.999512
P(x = 14)   =  0.000458          P(x <= 14)  =  0.999969
P(x = 15)   =  0.000031          P(x <= 15)  =  1.000000
Please enter the sample size:
Enter the probability of a certain behaviour occurring:
P(x = 0)    =     -3.712930          P(x <= 0)   =     -3.712930
P(x = 1)    =     32.845150          P(x <= 1)   =     29.132220
P(x = 2)    =   -116.221300          P(x <= 2)   =    -87.089080
P(x = 3)    =    205.622300          P(x <= 3)   =    118.533220
P(x = 4)    =   -181.896650          P(x <= 4)   =    -63.363430
P(x = 5)    =     64.363430          P(x <= 5)   =      1.000000
Please enter the sample size: