
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Probability of reaching a point with 2 or 3 steps at a time in C++
A person “A” is walking from a starting position X = 0, the task is to find the probability to reach exactly X = num, if he/she can either take 2 or 3 steps. Probability for step length 2 i.e. P, the probability for the step length 3 is 1 - P.
Input
num = 5, p = 0.2
Output
0.32
Explanation
There can be 2 ways to reach num, i.e, 5 2+3 with probability 0.2 * 0.8 = 0.16 3+2 with probability 0.8 * 0.2 = 0.16 So, total probability will be 0.16 + 0.16 = 0.32
Input
num = 2, p = 0.1
Output
0.1
Approach used below is as follows to solve the problem
We will be using dynamic programming approach to solve the problem.
In the solution we will −
Declare a probability array whose size is num+1 and assign its values as, Set probab[0] = 1, Set probab[1] = 0, Set probab[2] = p, Set probab[3] = 1 – p
Iterate i from 0 to num, while increment its value
For every i, Set probab[i] = (p)*probab[i - 2] + (1 - p) * probab[i - 3]
Return the probab[num]
Print result.
Algorithm
Start Step 1→ declare function to calculate probability of reaching a point with 2 or 3 steps at a time float probab(int num, float p) Declare double probab[num + 1] `Set probab[0] = 1 Set probab[1] = 0 Set probab[2] = p Set probab[3] = 1 – p Loop For int i = 4 and i <= num and ++i Set probab[i] = (p)*probab[i - 2] + (1 - p) * probab[i - 3] End return probab[num] Step 2→ In main() Declare int num = 2 Declare float p = 0.1 Call probab(num, p) Stop
Example
#include <bits/stdc++.h> using namespace std; //function to calculate probability of reaching a point with 2 or 3 steps at a time float probab(int num, float p){ double probab[num + 1]; probab[0] = 1; probab[1] = 0; probab[2] = p; probab[3] = 1 - p; for (int i = 4; i <= num; ++i) probab[i] = (p)*probab[i - 2] + (1 - p) * probab[i - 3]; return probab[num]; } int main(){ int num = 2; float p = 0.1; cout<<"probability is : "<<probab(num, p); return 0; }
Output
If run the above code it will generate the following output −
probability is : 0.1
- Related Articles
- Find the probability of reaching all points after N moves from point N in C++
- C++ program to find the probability of a state at a given time in a Markov chain
- A die is thrown. Find the probability of getting a multiple of 2 or 3.
- C++ Rat in a Maze with Multiple Steps or Jump Allowed
- Find the probability of a state at a given time in a Markov chain - Set 1 in Python
- Rat in a Maze with multiple steps or jump allowed?
- In a family of $3$ children calculate the probability of having at least one boy.
- Reaching Points in C++
- In a right \( \triangle A B C \) right-angled at \( C \), if \( D \) is the mid-point of \( B C \), prove that $B C^{2}=4(A D^{2}-A C^{2})$.
- Find Equal (or Middle) Point in a sorted array with duplicates in C++
- In fig. 3, ABC is a right triangle, right angled at C and D is the mid-point of BC, Prove that $( AB)^{2} =4( AD)^{2} -3( AC)^{2} .$"\n
- A die is thrown. Find the probability of getting 2 or 4.
- Find the probability that a number selected at random from the numbers \( 1,2,3, \ldots, 35 \) is a multiple of 3 or 5.
- In a simultaneous throw of a pair of dice, find the probability of getting 2 will not come either time.
- In right-angled triangle \( A B C \) in which \( \angle C=90 \), if \( D \) is the mid-point of \( B C \) prove that \( A B^{2}=4 A D^{2}-3 A C^{2} \).

Advertisements