Hallar la media de los elementos del arreglo.
Mean = (Sum of elements of the Array) / (Total no of elements in Array)
Ejemplos:
Input : 1 2 3 4 5 Output : 3 Input : 1 2 3 Output : 2
Para encontrar la media usando la recursividad, suponga que el problema ya está resuelto para N-1, es decir, tiene que encontrar para n
Sum of first N-1 elements = (Mean of N-1 elements)*(N-1) Mean of N elements = (Sum of first N-1 elements + N-th elements) / (N)
Nota: dado que la indexación de arrays comienza desde 0, accedemos al elemento N-ésimo usando A[N-1].
Implementación:
C++
// Recursive C++ program to find mean of array #include <iostream> using namespace std; // Function Definition of findMean function float findMean(int A[], int N) { if (N == 1) return (float)A[N-1]; else return ((float)(findMean(A, N-1)*(N-1) + A[N-1]) / N); } // Main Calling Function int main() { float Mean = 0; int A[] = {1, 2, 3, 4, 5}; int N = sizeof(A)/sizeof(A[0]); cout << " "<< findMean(A, N); return 0; } // this code is contributed by shivanisinghss2110
C
// Recursive C program to find mean of array #include<stdio.h> // Function Definition of findMean function float findMean(int A[], int N) { if (N == 1) return (float)A[N-1]; else return ((float)(findMean(A, N-1)*(N-1) + A[N-1]) / N); } // Main Calling Function int main() { float Mean = 0; int A[] = {1, 2, 3, 4, 5}; int N = sizeof(A)/sizeof(A[0]); printf("%.2f\n", findMean(A, N)); return 0; }
Java
// Recursive Java program to find mean of array class CalcMean { // Function Definition of findMean function static float findMean(int A[], int N) { if (N == 1) return (float)A[N-1]; else return ((float)(findMean(A, N-1)*(N-1) + A[N-1]) / N); } // main Function public static void main (String[] args) { float Mean = 0; int A[] = {1, 2, 3, 4, 5}; int N = A.length; System.out.println(findMean(A, N)); } }
Python3
# Recursive Python3 program to # find mean of array # Function Definition of findMean function def findMean(A, N): if (N == 1): return A[N - 1] else: return ((findMean(A, N - 1) * (N - 1) + A[N - 1]) / N) # Driver Code Mean = 0 A = [1, 2, 3, 4, 5] N = len(A) print(findMean(A, N)) # This code is contributed by Anant Agarwal.
C#
// Recursive C# program to find mean of array using System; class CalcMean { // Function Definition of findMean function static float findMean(int []A, int N) { if (N == 1) return (float)A[N - 1]; else return ((float)(findMean(A, N - 1) * (N - 1) + A[N - 1]) / N); } // Driver code public static void Main() { //float Mean = 0; int []A = {1, 2, 3, 4, 5}; int N = A.Length; Console.WriteLine(findMean(A, N)); } } // This code is contributed by Anant Agarwal.
PHP
<?php // Recursive PHP program to find mean of array // Function Definition of findMean function function findMean($A, $N) { if ($N == 1) return $A[$N - 1]; else return ((findMean($A, $N - 1) * ($N - 1) + $A[$N - 1]) / $N); } // Driver Code $Mean = 0; $A = array(1, 2, 3, 4, 5); $N = sizeof($A); echo findMean($A, $N); // This code is contributed by ajit. ?>
Javascript
<script> // Recursive Javascript program to find mean of array // Function Definition of findMean function function findMean(A, N) { if (N == 1) return A[N - 1]; else return ((findMean(A, N - 1) * (N - 1) + A[N - 1]) / N); } // float Mean = 0; let A = [1, 2, 3, 4, 5]; let N = A.length; document.write(findMean(A, N)); // This code is contributed by rameshtravel07. </script>
3
Este artículo es una contribución de Prakhar Agrawal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu 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