2

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!

Jaaadeee
  • 61
  • 1
  • 8
  • You have three `long int factorial(int)` methods, this should cause a compile time error. You should change that to just one method along the lines of `long int factorial(int num)`, then you need to code your `factorial` method. – Jonny Henly Oct 30 '15 at 01:19
  • Alright I didn't get any error but I could change it. But I want to know how I write the probability formula in the program because i'm confused on how I am supposed to integrate it... – Jaaadeee Oct 30 '15 at 01:22
  • What do you mean by factorial method? @JonnyHenly – Jaaadeee Oct 30 '15 at 01:26
  • Are you asking us how to implement `long int factorial (int N, int x, int p)`? What is the relevance of the rest of your code? – Neil Kirk Oct 30 '15 at 01:28
  • Let me try and clarify my question. I have this program to make and it has to calculate a certain probability when you enter the values of N, x and p. I was able to successfully code a part of my code that works (this part that works allows you to enter the values of N, x and p by respecting the initial problem stated on the top of the page). However, I don't know how to use these entered values in a way that it computes the following formula: () = (!) / (!) * (−)!) * (p^x) * ((1-p)^(N-x)). I want to be able Is it more clear now? – Jaaadeee Oct 30 '15 at 01:38

1 Answers1

1

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.

Community
  • 1
  • 1
JParks
  • 179
  • 1
  • 1
  • 13