Probability of a key K present in array | C ++ code to find the probability of search key K present in array ; Function to find the probability ; find probability ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float kPresentProbability ( int a [ ] , int n , int k ) { float count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == k ) count ++ ; return count / n ; } int main ( ) { int A [ ] = { 4 , 7 , 2 , 0 , 8 , 7 , 5 } ; int K = 3 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << kPresentProbability ( A , N , K ) ; return 0 ; }