Answer to Question #225955 in C++ for Ulando

Question #225955

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


1
Expert's answer
2021-08-18T06:51:22-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS