# 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

Live Demo

#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