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:
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:
Comments
Leave a comment