Program to find if two numbers and their AM and HM are present in an array using STL | C ++ program to check if two numbers are present in an array then their AM and HM are also present . Finally , find the GM of the numbers ; Function to find the Arithmetic Mean of 2 numbers ; Function to find the Harmonic Mean of 2 numbers ; Following function checks and computes the desired results based on the means ; Calculate means ; Hash container ( Set ) to store elements ; Insertion of array elements in the Set ; Conditionals to check if numbers are present in array by Hashing ; Conditionals to check if the AM and HM of the numbers are present in array ; If all conditions are satisfied , the Geometric Mean is calculated ; If numbers are found but the respective AM and HM are not found in the array ; If none of the conditions are satisfied ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float ArithmeticMean ( float A , float B ) { return ( A + B ) / 2 ; } float HarmonicMean ( float A , float B ) { return ( 2 * A * B ) / ( A + B ) ; } void CheckArithmeticHarmonic ( float arr [ ] , float A , float B , int N ) { float AM = ArithmeticMean ( A , B ) ; float HM = HarmonicMean ( A , B ) ; unordered_set < float > Hash ; for ( int i = 0 ; i < N ; i ++ ) { Hash . insert ( arr [ i ] ) ; } if ( Hash . find ( A ) != Hash . end ( ) && Hash . find ( B ) != Hash . end ( ) ) { if ( Hash . find ( AM ) != Hash . end ( ) && Hash . find ( HM ) != Hash . end ( ) ) { cout << " GM ▁ = ▁ " ; printf ( " % 0.2f " , sqrt ( AM * HM ) ) ; } else { cout << " AM ▁ and ▁ HM ▁ not ▁ found " ; } } else { cout << " Numbers ▁ not ▁ found " ; } } int main ( ) { float arr [ ] = { 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; float A = 3.0 ; float B = 6.0 ; CheckArithmeticHarmonic ( arr , A , B , N ) ; return 0 ; }