Jaaadeee
Jaaadeee

Reputation: 61

Adding a Probability Equation Into the Program

I've been working on this program in which it should calculate the probability based on the following formula:

𝑃(𝑥) = (𝑁!) / (𝑥!) * (𝑁−𝑥)!) * (p^x) * ((1-p)^(N-x))

Also, when the user types in a value, N must be an integer, x must be an integer which can be between 0 and N, and p must be a positive real number between 0 and 1. Till now this part works just fine but I don't know how to properly add the probability formula in the program.

The following is my code so far:

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


long int factorial (int N, int x, int p);


int main ()
{
double N, x, p;


cout << "Input N value" << endl;
cin >> N;

cout << "Input x Value" << endl;
cin >> x;
while(x<=0 || x>=N){
    cout << "x value is NOT between 0 and N." << endl;
    cout << "Input x Value" << endl;
    cin >> x;

}
cout << "Input p value" << endl;
cin >> p;
while(p<=0 || p>=1){
    cout << "p value is NOT a real number between 0 and 1." << endl;
    cout << "Input p value" << endl;
    cin >> p;
}
return 0;

}

Can anyone help me out just to understand how to properly add an equation in my program?

Thank you!

This is my new code:

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


double factorial (double N, double x, double p);


int main ()
{
double N;
double x;
double p;

cout << "Input N value" << endl;
cin >> N;

cout << "Input x Value" << endl;
cin >> x;
while(x<=0 || x>=N){
    cout << "x value is NOT between 0 and N." << endl;
    cout << "Input x Value" << endl;
    cin >> x;

}
cout << "Input p value" << endl;
cin >> p;
while(p<=0 || p>=1){
    cout << "p value is NOT a real number between 0 and 1." << endl;
    cout << "Input p value" << endl;
    cin >> p;
}
double Probability;
Probability = factorial(N, x, p);

cout << "Probability= " << Probability << endl;
return 0;
}

double factorial (double N, double x, double p){


double answer = ((tgamma(N+1))/((tgamma(x+1)) * (tgamma((N-x)+1)))) * (pow(p,x)) * (pow((1-p),(N-x)));
return answer;
}

The program recognizes the values I put in the system but when it calculates the answer, it gives a really small number. I tried out each section of the formula to make sure their was not a mistake but everything works fine when I tested it independently. Does anyone know what's wrong with the equation?

Thank you!

Upvotes: 2

Views: 1358

Answers (1)

JParks
JParks

Reputation: 179

First you need to write a factorial function, check out this stackoverflow link: How do you implement the factorial function in C++?

Then just write a function for your calculation. Assuming your factorial function is called getFact(int n) then:

double solve(int N, int x, double p) {

    double answer = ( getFact(N)/getFact(x) )*getFact((N-x))* pow(p,x)* pow((1-p),(N-x));
    return answer;
}

Then call the solve function in your main after having set your values.

double P_x;
P_x = solve(N,x,p);

Also, I use doubles because they can be more accurate, especially for p since its is 0 <= p <= 1.

Upvotes: 1

Related Questions