Se da una array de números naturales y otra array con el peso correspondiente del número. Luego tenemos que calcular la media ponderada.
Donde x (barra) se llama la media ponderada, x[i] son los elementos de la array y W[i] es el peso de los elementos de la array x[i].
Ejemplos:
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
Implementación: Una solución simple para resolver la media ponderada.
C++
// 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; }
Java
// JAVA Program to find weighted mean // of natural numbers. class GFG { // Function to calculate weighted mean. static 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. public static void main(String args[]) { // 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 = X.length; int m = W.length; // Check the size of both array is // equal or not. if (n == m) System.out.println(weightedMean(X, W, n)); else System.out.println("-1" ); } } /*This code is contributed by Nikita Tiwari.*/
Python
# Python Program to find weighted mean of # natural numbers. # Function to calculate weighted mean. def weightedMean(X,W,n) : sum = 0 numWeight = 0 i = 0 while i < n : numWeight = numWeight + X[i] * W[i] sum = sum + W[i] i = i + 1 return (float)(numWeight / sum) # Driver program to test the function. # Take num array and corresponding weight # array and initialize it. X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] W = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Calculate the size of array. n = len(X) m = len(W) # Check the size of both array is equal or not. if (n == m) : print weightedMean(X, W, n) else : print "-1" # This code is contributed by Nikita Tiwari.
C#
// C# Program to find weighted mean // of natural numbers. using System; class GFG { // Function to calculate weighted mean. static 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. public static void 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 = X.Length; int m = W.Length; // Check the size of both array is // equal or not. if (n == m) Console.WriteLine(weightedMean(X, W, n)); else Console.WriteLine("-1" ); } } // This code is contributed by vt_m.
PHP
<?php // Program to find weighted mean of // natural numbers. // Function to calculate // weighted mean. function weightedMean($X, $W, $n) { $sum = 0; $numWeight = 0; for ($i = 0; $i < $n; $i++) { $numWeight = $numWeight + $X[$i] * $W[$i]; $sum = $sum + $W[$i]; } return (float)($numWeight / $sum); } // Driver Code // Take num array and corresponding weight // array and initialize it. $X = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $W = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Calculate the size of array. $n = sizeof($X); $m = sizeof($W); // Check the size of both // array is equal or not. if ($n == $m) echo(weightedMean($X, $W, $n)); else echo("-1"); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript program to find weighted mean // of natural numbers. // Function to calculate weighted mean. function weightedMean(X, W, n) { let sum = 0, numWeight = 0; for(let i = 0; i < n; i++) { numWeight = numWeight + X[i] * W[i]; sum = sum + W[i]; } return (numWeight) / sum; } // Driver code // Take num array and corresponding // weight array and initialize it. let X = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; let W = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; // Calculate the size of array. let n = X.length; let m = W.length; // Check the size of both array is // equal or not. if (n == m) document.write(weightedMean(X, W, n)); else document.write("-1" ); // This code is contributed by susmitakundugoaldanga </script>
7
Segundo método: para calcular la media ponderada de los primeros n números naturales. Es la fórmula para calcular la media ponderada de los primeros n números naturales. En este método, hemos dado primero n número natural y su peso también son los números naturales. Luego generamos la 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
Implementación:
C++
// 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; }
Java
// Program to find weighted mean of first // n natural numbers using formula. import java.io.*; public class GFG { // Returns weighted mean assuming for numbers // {1, 2, ..n} and weights {1, 2, .. n} static int weightedMean(int n) { return (2 * n + 1)/3; } // Driver program to test the function. static public void main (String[] args) { int n = 10; System.out.println(weightedMean(n)); } } // This code is contributed by vt_m.
Python3
# Program to find weighted mean of first # n natural numbers using formula. # Returns weighted mean assuming for numbers # 1, 2, ..n and weights 1, 2, .. n def weightedMean(n): return (2 * n + 1) / 3 # Driver program to test the function. n = 10 print(int(weightedMean(n))) # This code is contributed by smita
C#
// Program to find weighted mean of first // n natural numbers using formula. using System; public class GFG { // Returns weighted mean assuming for numbers // {1, 2, ..n} and weights {1, 2, .. n} static int weightedMean(int n) { return (2 * n + 1) / 3; } // Driver program to test the function. static public void Main () { int n = 10; Console.WriteLine(weightedMean(n)); } } // This code is contributed by vt_m.
PHP
<?php // Program to find weighted mean of first // n natural numbers using formula. // Returns weighted mean // assuming for numbers // {1, 2, ..n} and // weights {1, 2, .. n} function weightedMean($n) { return (2 * $n + 1) / 3; } // Driver Code $n = 10; echo(weightedMean($n)); // This code is contributed by Ajit. ?>
Javascript
<script> // Program to find weighted mean of first // n natural numbers using formula. // Returns weighted mean assuming for numbers // {1, 2, ..n} and weights {1, 2, .. n} function weightedMean(n) { return parseInt((2 * n + 1) / 3, 10); } let n = 10; document.write(weightedMean(n)); </script>
7
Este artículo es una contribución de Dharmendra Kumar . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA