

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Probability of rain on N+1th day in C++
Given with an array containing 0’s and 1’s where 0’s represents no rain and 1’s represent rainy day. The task is to calculate the probability of rain on N+1th day.
To calculate the probability of rain on N+1th day we can apply the formula
Total number of rainy days in the set / total number of days in a
Input
arr[] = {1, 0, 0, 0, 1 }
Output
probability of rain on n+1th day : 0.4
Explanation
total number of rainy and non-rainy days are: 5 Total number of rainy days represented by 1 are: 2 Probability of rain on N+1th day is: 2 / 5 = 0.4
Input
arr[] = {0, 0, 1, 0}
Output
probability of rain on n+1th day : 0.25
Explanation
total number of rainy and non-rainy days are: 4 Total number of rainy days represented by 1 are: 1 Probability of rain on N+1th day is: 1 / 4 = 0.25
Approach used in the given program is as follows
Input the elements of an array
Input 1 for representing rainy day
Input 0 for representing non-rainy day
Calculate the probability by applying the formula given above
Print the result
Algorithm
Start Step 1→ Declare Function to find probability of rain on n+1th day float probab_rain(int arr[], int size) declare float count = 0, a Loop For int i = 0 and i < size and i++ IF (arr[i] == 1) Set count++ End End Set a = count / size return a step 2→ In main() Declare int arr[] = {1, 0, 0, 0, 1 } Declare int size = sizeof(arr) / sizeof(arr[0]) Call probab_rain(arr, size) Stop
Example
#include <bits/stdc++.h> using namespace std; //probability of rain on n+1th day float probab_rain(int arr[], int size){ float count = 0, a; for (int i = 0; i < size; i++){ if (arr[i] == 1) count++; } a = count / size; return a; } int main(){ int arr[] = {1, 0, 0, 0, 1 }; int size = sizeof(arr) / sizeof(arr[0]); cout<<"probability of rain on n+1th day : "<<probab_rain(arr, size); return 0; }
Output
If run the above code it will generate the following output −
probability of rain on n+1th day : 0.4
- Related Questions & Answers
- Probability of getting a sum on throwing 2 Dices N times in C++
- Maximizing Probability of one type from N containers in C++
- Find the probability of reaching all points after N moves from point N in C++
- Probability of getting at least K heads in N tosses of Coins in C++
- C++ Queries on Probability of Even or Odd Number in Given Ranges
- Probability that the pieces of a broken stick form a n sided polygon in C++
- Subjective Probability Vs. Objective Probability
- Trapping Rain Water in Python
- Knight Probability in Chessboard in C++
- A matrix probability question in C?
- Airplane Seat Assignment Probability in C++
- Day of the Week in C++
- Crontab day of the week syntax on Linux
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Probability of a key K present in array in C++
Advertisements