- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 Articles
- Probability of getting a sum on throwing 2 Dices N times in C++
- The probability that it will rain tomorrow is 0.85. What is the probability that it will not rain tomorrow?
- 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++
- Probability that the pieces of a broken stick form a n sided polygon in C++
- C++ Queries on Probability of Even or Odd Number in Given Ranges
- Day of the Week in C++
- Maximum bishops that can be placed on N*N chessboard in C++
- What is acid rain? How is acid rain caused? What are the harmful effects of acid rain?
- Crontab day of the week syntax on Linux
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Two customers are visiting a particular shop in the same week (Monday to Saturday). Each is equally likely to visit the shop on any one day as on another. What is the probability that both will visit the shop on the same day?
- A solar water heater cannot be used to get hot water on$(a)$ a sunny day$(b)$ a cloudy day$(c)$ a hot day$(d)$ a windy day
- A boy of mass 50 kg standing on ground exerts a force of 500 N on the ground. The force exerted by the ground on the boy will be:(a) 50 N(b) 25000 N(c) 10 N(d) 500 N

Advertisements