📜  数组平均程序(迭代和递归)

📅  最后修改于: 2022-05-13 01:57:47.588000             🧑  作者: Mango

数组平均程序(迭代和递归)

给定一个数组,任务是找到该数组的平均值。平均值是数组元素的总和除以元素的数量。

例子 :

Input : arr[] = {1, 2, 3, 4, 5}
Output : 3
Sum of the elements is 1+2+3+4+5 = 15 
and total number of elements is 5.
So average is 15/5 = 3

Input : arr[] = {5, 3, 6, 7, 5, 3}
Output : 4.83333
Sum of the elements is 5+3+6+7+5+3 = 29
and total number of elements is 6.
So average is 29/6 = 4.83333.

迭代:
迭代程序很容易。我们需要找到总和并将总和除以元素总数。

C++
// C++ program to calculate average of array elements
#include 
using namespace std;
 
// Function that return average of an array.
double average(int a[], int n)
{
    // Find sum of array element
    int sum = 0;
    for (int i=0; i


Java
// Java program to calculate average of array elements
class GFG {
     
    // Function that return average of an array.
    static double average(int a[], int n)
    {
         
        // Find sum of array element
        int sum = 0;
         
        for (int i = 0; i < n; i++)
            sum += a[i];
     
        return (double)sum / n;
    }
     
    //driver code
    public static void main (String[] args)
    {
         
        int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.length;
     
        System.out.println(average(arr, n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 code to calculate
# average of array elements
 
# Function that return
# average of an array.
def average( a , n ):
 
    # Find sum of array element
    sum = 0
    for i in range(n):
        sum += a[i]
     
    return sum/n;
 
# Driver code
arr = [10, 2, 3, 4, 5, 6, 7, 8, 9]
n = len(arr)
print(average(arr, n))
 
# This code is contributed by "Shard_Bhardwaj".


C#
// C# program to calculate average of
// array elements
using System;
class GFG {
     
    // Function that return average of
    // an array.
    static double average(int []a, int n)
    {
         
        // Find sum of array element
        int sum = 0;
         
        for (int i = 0; i < n; i++)
            sum += a[i];
     
        return (double)sum / n;
    }
     
    // Driver code
    public static void Main ()
    {
         
        int []arr = {10, 2, 3, 4, 5, 6,
                                7, 8, 9};
        int n = arr.Length;
     
        Console.Write(average(arr, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C++ program to calculate average of array elements
#include 
using namespace std;
 
// Recursively computes average of a[]
double avgRec(int a[], int i, int n)
{
    // Last element
    if (i == n-1)
        return a[i];
 
    // When index is 0, divide sum computed so
    // far by n.
    if (i == 0)
        return ((a[i] + avgRec(a, i+1, n))/n);
 
    // Compute sum
    return (a[i] + avgRec(a, i+1, n));
}
 
// Function that return average of an array.
double average(int a[], int n)
{
     return avgRec(a, 0, n);
}
 
// Driver code
int main()
{
    int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << average(arr, n) << endl;
    return 0;
}


Java
// Java program to calculate average
// of array elements
 
class CalcAvg
{
    // Recursively computes average of a[]
    static double avgRec(int a[], int i, int n)
    {
        // Last element
        if (i == n-1)
            return a[i];
      
        // When index is 0, divide sum computed so
        // far by n.
        if (i == 0)
            return ((a[i] + avgRec(a, i+1, n))/n);
      
        // Compute sum
        return (a[i] + avgRec(a, i+1, n));
    }
      
    // Function that return average of an array.
    static double average(int a[], int n)
    {
         return avgRec(a, 0, n);
    }
     
    // main function
    public static void main (String[] args)
    {
        int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.length;
      
        System.out.println(average(arr, n));
    }
}


Python3
# Python3 code to calculate
# average of array elements
 
# Recursively computes average of a[]
def avgRec( a , i , n ):
     
    # Last element
    if i == n-1:
        return a[i]
     
    # When index is 0, divide sum
    # computed so far by n.
    if i == 0:
        return ((a[i] + avgRec(a, i+1, n)) / n)
     
    # Compute sum
    return (a[i] + avgRec(a, i+1, n))
 
# Function that return
# average of an array.
def average(a, n):
    return avgRec(a, 0, n)
 
# Driver code
arr = [10, 2, 3, 4, 5, 6, 7, 8, 9]
n = len(arr)
print(average(arr, n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to calculate average
// of array elements
using System;
 
class GFG {
     
    // Recursively computes average of a[]
    static double avgRec(int []a, int i, int n)
    {
         
        // Last element
        if (i == n-1)
            return a[i];
     
        // When index is 0, divide sum
        // computed so far by n.
        if (i == 0)
            return ((a[i] + avgRec(a, i+1, n))/n);
     
        // Compute sum
        return (a[i] + avgRec(a, i+1, n));
    }
     
    // Function that return average of an array.
    static double average(int []a, int n)
    {
        return avgRec(a, 0, n);
    }
     
    // main function
    public static void Main ()
    {
        int []arr = {10, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.Length;
     
        Console.Write(average(arr, n));
    }
}
 
// This code is contributed by Smitha.


PHP


Javascript


输出
6

时间复杂度: O(n)

递归:
这个想法是将元素的索引作为附加参数传递并递归计算总和。计算总和后,将总和除以 n。

C++

// C++ program to calculate average of array elements
#include 
using namespace std;
 
// Recursively computes average of a[]
double avgRec(int a[], int i, int n)
{
    // Last element
    if (i == n-1)
        return a[i];
 
    // When index is 0, divide sum computed so
    // far by n.
    if (i == 0)
        return ((a[i] + avgRec(a, i+1, n))/n);
 
    // Compute sum
    return (a[i] + avgRec(a, i+1, n));
}
 
// Function that return average of an array.
double average(int a[], int n)
{
     return avgRec(a, 0, n);
}
 
// Driver code
int main()
{
    int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    cout << average(arr, n) << endl;
    return 0;
}

Java

// Java program to calculate average
// of array elements
 
class CalcAvg
{
    // Recursively computes average of a[]
    static double avgRec(int a[], int i, int n)
    {
        // Last element
        if (i == n-1)
            return a[i];
      
        // When index is 0, divide sum computed so
        // far by n.
        if (i == 0)
            return ((a[i] + avgRec(a, i+1, n))/n);
      
        // Compute sum
        return (a[i] + avgRec(a, i+1, n));
    }
      
    // Function that return average of an array.
    static double average(int a[], int n)
    {
         return avgRec(a, 0, n);
    }
     
    // main function
    public static void main (String[] args)
    {
        int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.length;
      
        System.out.println(average(arr, n));
    }
}

Python3

# Python3 code to calculate
# average of array elements
 
# Recursively computes average of a[]
def avgRec( a , i , n ):
     
    # Last element
    if i == n-1:
        return a[i]
     
    # When index is 0, divide sum
    # computed so far by n.
    if i == 0:
        return ((a[i] + avgRec(a, i+1, n)) / n)
     
    # Compute sum
    return (a[i] + avgRec(a, i+1, n))
 
# Function that return
# average of an array.
def average(a, n):
    return avgRec(a, 0, n)
 
# Driver code
arr = [10, 2, 3, 4, 5, 6, 7, 8, 9]
n = len(arr)
print(average(arr, n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# program to calculate average
// of array elements
using System;
 
class GFG {
     
    // Recursively computes average of a[]
    static double avgRec(int []a, int i, int n)
    {
         
        // Last element
        if (i == n-1)
            return a[i];
     
        // When index is 0, divide sum
        // computed so far by n.
        if (i == 0)
            return ((a[i] + avgRec(a, i+1, n))/n);
     
        // Compute sum
        return (a[i] + avgRec(a, i+1, n));
    }
     
    // Function that return average of an array.
    static double average(int []a, int n)
    {
        return avgRec(a, 0, n);
    }
     
    // main function
    public static void Main ()
    {
        int []arr = {10, 2, 3, 4, 5, 6, 7, 8, 9};
        int n = arr.Length;
     
        Console.Write(average(arr, n));
    }
}
 
// This code is contributed by Smitha.

PHP


Javascript


输出
6

相关文章:
数字流的平均值