Write a C++ program that creates a one dimensional array of length n. Your program should read five integers into the array from the keyboard and perform a traverse operation of finding the homonic mean of each element in thearray
#include <bits/stdc++.h>
using namespace std;
//calculate harmonic mean
float harmonic_mean(int arr[], int length){
float sum = 0;
for (int i = 0; i < length; i++)
sum = sum + (float)1 / arr[i];
return (float)length/sum;
}
int main(){
// we declare an one-dimensional array that accepts n length
int n=5;
int arr[n];
// we should enter the elements of array from keyboard
cout << "Enter 5 integer numbers: " << endl;
for (int i = 0; i < n; i++){
cin >> arr[i];
}
//print out the array integers entered from the keyboard
cout << "The entered array is : " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
//finding the homonic mean of each element in thearray
int length=5;
cout<<"\nHarmonic mean is : "<<harmonic_mean(arr, length);
return 0;
}
Comments
Leave a comment