Knowee
Questions
Features
Study Tools

Q4 - Average Of all Array ElementsWrite a Program to Find the average of all elements in a Given Array?Constraints:Input          :- First Line of Input Consists of One Integer Value (Array Size).                     Second Line of Input Consists of few Integer Values Separated by Space (Array Elements).Output        :- Print the Average of All the Array Elements.Constraints  :- Array Size must be Greater then 0 or else Print "Invalid ArRay Size.".                      Print the Average value upto 5 Decimal points.Example:Input 1  :    6                  10 02 20 21 11 54Output 1:    Average of Given Array Elements is 19.66666. Input 2  :    4                  1 2 3 0Output 2:    Average of Given Array Elements is 1.50000.Explanation:Input 1  :    6                  10 02 20 21 11 54Output 1:    19.66666Explanation:                  Sum = 10 + 02 + 20 + 21 + 11 + 54                          = 118                  Average = 118 / 6 = 19.66666. Input 2  :    4                  1 2 3 0Output 2:    1.50000.Explanation:                  Sum = 1 + 2 + 3 + 0                          = 6                  Average = 6 / 4 = 1.55555.

Question

Q4 - Average Of all Array ElementsWrite a Program to Find the average of all elements in a Given Array?Constraints:Input          :- First Line of Input Consists of One Integer Value (Array Size).                     Second Line of Input Consists of few Integer Values Separated by Space (Array Elements).Output        :- Print the Average of All the Array Elements.Constraints  :- Array Size must be Greater then 0 or else Print "Invalid ArRay Size.".                      Print the Average value upto 5 Decimal points.Example:Input 1  :    6                  10 02 20 21 11 54Output 1:    Average of Given Array Elements is 19.66666. Input 2  :    4                  1 2 3 0Output 2:    Average of Given Array Elements is 1.50000.Explanation:Input 1  :    6                  10 02 20 21 11 54Output 1:    19.66666Explanation:                  Sum = 10 + 02 + 20 + 21 + 11 + 54                          = 118                  Average = 118 / 6 = 19.66666. Input 2  :    4                  1 2 3 0Output 2:    1.50000.Explanation:                  Sum = 1 + 2 + 3 + 0                          = 6                  Average = 6 / 4 = 1.55555.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python program that calculates the average of all elements in a given array:

# Get the size of the array
n = int(input())

# Check if the size is greater than 0
if n > 0:
    # Get the array elements
    arr = list(map(int, input().split()))
    
    # Calculate the sum of the array elements
    sum_arr = sum(arr)
    
    # Calculate the average
    average = sum_arr / n
    
    # Print the average up to 5 decimal points
    print("Average of Given Array Elements is %.5f." % average)
else:
    print("Invalid Array Size.")

This program first reads the size of the array. If the size is greater than 0, it reads the array elements, calculates the sum of these elements, and then calculates the average by dividing the sum by the number of elements. The average is then printed up to 5 decimal points. If the size of the array is not greater than 0, it prints "Invalid Array Size."

This problem has been solved

Similar Questions

Q5 - Average of All Prime Numbers in Given ArrayWrite a Program to Find the average of all Prime Numbers from a Given Array?Constraints:Input          :- First Line of Input Consists of One Integer Value (Array Size).                     Second Line of Input Consists of few Integer Values Separated by Space (Array Elements).Output        :- Print the Average of All Prime Number from the Array Elements.Constraints  :- Array Size must be Greater than 3 or else Print "Invalid Array Size.".                      Print the Average value upto 3 Decimal points.                      All the Given Array Values Must be greater than ONE, or else print "Invalid Array Elements".                       If there is no Prime Numbers in a Given Array then Print "No Prime Numbers!".Example:Input 1  :    6                  10 02 20 21 11 54Output 1:     Average of Prime Numbers in a Given Array Elements is 6.500. Input 2  :    4                  55 2 3 11Output 2:  Average of Prime Numbers in a Given Array Elements is 5.333.Explanation:Input 1  :    6                  10 02 20 21 11 54Output 1:    Average of Prime Numbers in a Given Array Elements is 6.500.Explanation:                  Prime Numbers in a Given Array is 02 11.                  Sum of Prime Numbers in a Given Array Elements = 02 + 11 = 13                  Average of Prime Numbers in a Given Array Elements = 13 / 2 = 6.500. Input 2  :    4                  55 2 3 11Output 2:    Average of Prime Numbers in a Given Array Elements is 5.333.Explanation:                  Prime Numbers in a Given Array is 2 3 11.                  Sum of Prime Numbers in a Given Array Elements = 2 + 3 + 11 = 16                  Average of Prime Numbers in a Given Array Elements = 16 / 3 = 5.333.Public Test Cases:# INPUT EXPECTED OUTPUT1 610 02 20 21 11 54Average of Prime Numbers in a Given Array Elements is 6.500.2 455 2 3 11Average of Prime Numbers in a Given Array Elements is 5.333.

Write a Program to Find the average of all Prime Numbers from a Given Array?

Initialize an array/list with five integers. Write a program to find the sumand average of these numbers

Problem StatementWrite a program to find the average of N numbers. Print the average in 2 digit precision DO NOT USE ARRAYInput FormatFirst line consist of a single integer - 'N'Second line consist of N space separated integersOutput FormatDisplay the average of N numbersSample Input51 2 3 4 5Sample Output3.00

Given a matrix, write a program to calculate the average of all its elements.Input:The input consists of two integers, m and n, representing the number of rows and columns in the matrix, respectively. This is followed by m lines, each containing n integers representing the elements of the matrix.Output:Print the average of all elements in the matrix.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.