# Program for weighted mean of natural numbers.

There are given an array of natural numbers and another array with corresponding weight of the number. Then we have to calculate the weighted mean.

Where x (bar) is called the weighted mean, x[i] is the elements of array, and W[i] is the weight of elements of array x[i].

Examples –

```Input :
X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
weighted mean
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + 10 * 10) /
(1 + 2 + 3 + . . . + 10)
= 385 / 55 = 7
Output : 7

Input :
X[] = {3, 4, 5, 6, 7}
W[] = {4, 5, 6, 7, 8}
weighted mean
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (3 * 4 + 4 * 5 + 5 * 6 + 6 * 7 + 7 * 8) /
(4 + 5 + 6 + 7 + 8)
= 160 / 30 = 5.33333
Output : 5.33333```

A simple solution to solve weighed mean.

 ` `
`// Program to find weighted mean of`
`// natural numbers.`
`#include<bits/stdc++.h>`
`using` `namespace` `std;`
`// Function to calculate weighted mean.`
`float` `weightedMean(``int` `X[], ``int` `W[], ``int` `n)`
`{`
`    ``int` `sum = 0, numWeight = 0;`
`    ``for` `(``int` `i = 0; i < n; i++)`
`    ``{`
`        ``numWeight = numWeight + X[i] * W[i];`
`        ``sum = sum + W[i];`
`    ``}`
`    ``return` `(``float``)numWeight / sum;`
`}`
`// Driver program to test the function.`
`int` `main()`
`{`
`    ``// Take num array and corresponding weight  `
`    ``// array and initialize it.`
`    ``int` `X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};`
`    ``int` `W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};`
`    ``// Calculate the size of array.`
`    ``int` `n = ``sizeof``(X)/``sizeof``(X[0]);`
`    ``int` `m = ``sizeof``(W)/``sizeof``(W[0]);`
`    ``// Check the size of both array is equal or not.`
`    ``if` `(n == m)`
`        ``cout << weightedMean(X, W, n);`
`    ``else`
`        ``cout << ``"-1"``;`
`    ``return` `0;`
`}`

Output –

```7
```

Second method – to compute the weighted mean of first n natural numbers. It is the formula to compute the weighted mean of first n natural numbers. In this method, we have given first n natural number and their weight are also be the natural numbers. Then we generate the formula

```Weighted Mean
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + n * n) / (1 + 2 + 3 + . . . + n)
= (n * (n + 1) * (2 * n + 1) / 6) / (n * (n + 1) / 2)

Weighted Mean = (2 * n + 1) / 3

Example: Weighted mean of first 10 natural numbers
n = 10
Weighted mean
= (2 * 10 + 1) / 3 = 21 / 3 = 7
```
`// Program to find weighted mean of first`
`// n natural numbers using formula.`
`#include<bits/stdc++.h>`
`using` `namespace` `std;`
`// Returns weighted mean assuming for numbers`
`// {1, 2, ..n} and weights {1, 2, .. n}`
`int` `weightedMean(``int` `n)`
`{`
`    ``return` `(2 * n + 1)/3;`
`}`
`// Driver program to test the function.`
`int` `main()`
`{`
`    ``int` `n = 10;`
`    ``cout << weightedMean(n);`
`    ``return` `0;`
`}`

Output –

```7
```

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at