#include<iostream>
#include<cmath>
using namespace std;
float san= 0.25 ; float var= 0.75;
int findFact(int n)//factorial
{
return n == 1 ? 1 : n * findFact(n - 1);
}
int findNcR(int n, int r)//combination nCr
{
return findFact(n) / (findFact(n - r) * findFact(r));
}
double prob(int s, int v){ //recursive function for probability
if(s>=5) return 1; if(v>=5) return 0;
double sum = 0;
int m = 5-s;
for( int i=0; i<=m; i ){
sum = prob(s i,v m-i)*findNcR(m,i)*pow(san,i)*pow(var,m-i);
}
return sum;
}
int main(){
cout<< prob(2,1);
}
In DEV C , there is no output printed when I compile and run the above code. I think its because of large fractional values involved. Any idea how I can get the output?
CodePudding user response:
The base case for your recursion, s==5
or v==5
is never getting hit. As you call your function with s=2
, every time the prob
function is called it is setting m
to 3
, and so on the first iteration of the loop (when i==0
) it calls prob
with s=2
and v=v 3
. As you start with v==1
, it successively calls prob(2,1)
, prob(2,4)
, prob(2,7)
, etc... and never gets any further.
I don't know what probability distribution you are trying to code so I can't offer any specific advice on how to fix this.
CodePudding user response:
Please check the logic you use in your double prob(int s, int v)
method.
You are going to infinity recursive like
S=2 V=1 S=2 V=4 S=2 V=7