Answer to Question #182384 in Python for FORTUNE AIDOO

Question #182384

Part 2

Write your own function that illustrates a feature that you learned in this unit. The function must take at least one argument. The function should be your own creation, not copied from any other source. Do not copy a function from your textbook or the Internet.

Include all of the following in your Learning Journal:

  • The code for the function that you invented.
  • The inputs and outputs to three calls of your invented function.
  • A description of what feature(s) your function illustrates.  
1
Expert's answer
2021-04-19T01:40:02-0400

Description: In the given python code, a function named as CalcMean(u) is created to calculate the mean of an array. The array is passed as u in the function from the main function.

The function CalcMean first calculates the length of the array . Then the a temporary variable Sum is initialized to zero and sum of all the elements of array u are taken in the for loop that goes from 0 to length of the array u. In the end, the sum is divided by the length of the array to get the mean. The mean is returned to the calling function.


def CalcMean(u):
    Sum=0
    for r in range(0,len(u)):
        Sum = Sum + u[r]
    Mean = Sum/len(u)
    return(Mean)


A = [2, 3, 12, 56]
m = CalcMean(A)
print("\nCall No. 1")
print("Input Array = ",A)
print("Mean = %.2f"%m)

A = [20, 13, 12, 56]
m = CalcMean(A)
print("\nCall No. 2")
print("Input Array = ",A)
print("Mean = %.2f"%m)

A = [4, 33, 22, 560]
m = CalcMean(A)
print("\nCall No. 3")
print("Input Array = ",A)
print("Mean = %.2f"%m)


Python Output: Three different calls:


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS