Arithmetic Mean in c++?

C++Server Side ProgrammingProgramming

The Arithmetic Mean is just the average of numbers. In this program we will see how we can find the arithmetic mean from a set of numbers. The function will take the number set, and the number of elements. Out task is just adding each element, then divide it by number of elements that are passed.

Algorithm

arithmeticMean(dataset, n)

begin
   sum := 0
   for each element e from dataset, do
      sum := sum + e
   done
   return sum/n
end

Example

 Live Demo

#include<iostream>
using namespace std;
float arithmetic_mean(float data[], int size) {
   float sum = 0;
   for(int i = 0; i<size; i++) {
      sum += data[i];
   }
   return sum/size;
}
main() {
   float data_set[] = {25.3, 45.21, 78.56, 96.21, 22.12, 36.97};
   cout << "Mean: " << arithmetic_mean(data_set, 6);
}

Output

Mean: 50.7283
raja
Published on 25-Jul-2019 10:44:00
Advertisements