C++: Root Mean Square

Bjarne-stroustrup
 

Compute the Root mean square of the numbers 1..10.

The root mean square is also known by its initial RMS (or rms), and as the quadratic mean.

The RMS is calculated as the mean of the squares of the numbers, square-rooted:

x_{\mathrm{rms}} = \sqrt {{{x_1}^2 + {x_2}^2 + \cdots + {x_n}^2} \over n}.

#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>

int main( ) {
	std::vector<int> numbers ;
	for ( int i = 1 ; i < 11 ; i++ )
	numbers.push_back( i ) ;
	double meansquare = sqrt( ( std::inner_product( numbers.begin(), numbers.end(), numbers.begin(), 0 ) ) / static_cast<double>( numbers.size() ) );
	std::cout << "The quadratic mean of the numbers 1 .. " << numbers.size() << " is " << meansquare << " !\n" ;
	return 0 ;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.