Find the probability of reaching all points after N moves from point N | C ++ program to calculate the probability of reaching all points after N moves from point N ; Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; when the person moves from ith index in right direction when i moves has been done ; when the person moves from ith index in left direction when i moves has been done ; Print the arr ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printProbabilities ( int n , double left ) { double right = 1 - left ; double arr [ n + 1 ] [ 2 * n + 1 ] = { { 0 } } ; arr [ 1 ] [ n + 1 ] = right ; arr [ 1 ] [ n - 1 ] = left ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= 2 * n ; j ++ ) arr [ i ] [ j ] += ( arr [ i - 1 ] [ j - 1 ] * right ) ; for ( int j = 2 * n - 1 ; j >= 0 ; j -- ) arr [ i ] [ j ] += ( arr [ i - 1 ] [ j + 1 ] * left ) ; } for ( int i = 0 ; i < 2 * n + 1 ; i ++ ) printf ( " % 5.4f ▁ " , arr [ n ] [ i ] ) ; } int main ( ) { int n = 2 ; double left = 0.5 ; printProbabilities ( n , left ) ; return 0 ; }