
- 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 that the pieces of a broken stick form a n sided polygon in C++
We are given with the stick of any length and that stick can be broken randomly into n pieces which can be of type integer or floating point and the task is to find whether the broken pieces can form a n sided polygon.
We can calculate the probability by applying the formula
$$P(E^{\prime})=1-P(E)=1-\frac{n}{2^{n-1}}$$
Where, n is the number of pieces generated by breaking the stick into parts.
Input
length = 10 , pieces = 4
Output
probability is : 0.5
Explanation − given with length of size 10 cm and it is broken into 4 parts
Input
length = 5 , pieces = 3
Output
probability is : 0.25
Explanation − given with length of size 5 cm and it is broken into 3 parts
Approach used in the below program is as follows
Input the length of the stick with number of pieces it can be broken into
Apply the formula to calculate the probability
Print the result
Algorithm
Start Step 1→ Declare function to calculate the probability double probab(unsigned len, unsigned pieces) declare unsigned a = (1 << (pieces-1)) return 1.0 - ((double)pieces) / ((double)a) step 2→ In main() Declare unsigned pieces = 4, len = 10 Call probab(len, pieces) Stop
Example
#include<iostream> using namespace std; //function to calculate probability double probab(unsigned len, unsigned pieces){ unsigned a = (1 < (pieces-1)); return 1.0 - ((double)pieces) / ((double)a); } int main(){ unsigned pieces = 4, len = 10; cout <<"probability is : "<<probab(len, pieces); return 0; }
Output
If run the above code it will generate the following output −
probability is : 0.5
- Related Articles
- Apothem of a n-sided regular polygon in C++
- Area of a n-sided regular polygon with given Radius?
- Area of a n-sided regular polygon with given side length in C++
- Area of a n-sided regular polygon with given Radius in C Program?
- Find number of diagonals in n sided convex polygon in C++
- Determine the position of the third person on regular N sided polygon in C++?
- Area of largest Circle inscribe in N-sided Regular polygon in C Program?
- Area of largest Circle inscribed in N-sided Regular polygon in C Program?
- Determine the position of the third person on regular N sided polygon in C++ Program
- A tree of height 4.8 m is broken into three pieces in the ratio 3 : 4 : 5. Find the length of each piece.
- Boojho has learnt that non-metals on beating with a hammer are generally broken into pieces. Which of the following is non-metal?Iron nailAluminium wireCopper platePiece of coal
- Area of a polygon with given n ordered vertices in C++
- Count pieces of the circle after N cuts in C++
- Maximum number of pieces in N cuts in C++
- C++ program to count number of minutes needed to increase stick length to form a triangle
