105k views
1 vote
In Coral Language

Define a function named computeAverage that takes one parameter: an array of integer and outputs an integer that is the average of the all integers in the array Write a main program that reads integers from input. The first integer is the size of an array. All other integers will be values of the array. Then the program calls function computeAverage with the array as arguments. Function computeAverage will return the average of all integers in the array. Then the main program output the result Ex: if the input is: 5 2 1 6 2 4 where 5 is the size of the array, the output is: 3.0 Your program should define and use a function: Function computeAverage(integer array(?) userVals) returns float average Note: To avoid confusion with different ways of implementing this code and the truncation that occurs when doing integer division, all the tests for this lab will be whole numbers. You can use: Put yourvariable with 1 decimal place to display the result with one decimal place (which will end with .0).

User Kya
by
7.8k points

1 Answer

5 votes

Final answer:

In the Coral Language, you can define a function named computeAverage that takes one parameter: an array of integers. This function will calculate the average of all the integers in the array.

Step-by-step explanation:

In the Coral Language, you can define a function named computeAverage that takes one parameter: an array of integers. This function will calculate the average of all the integers in the array. Here's an example implementation:



def computeAverage(userVals):
sum = 0
for num in userVals:
sum += num
average = sum / len(userVals)
return average

# Main program
size = int(input())
array = []
for i in range(size):
array.append(int(input()))

result = computeAverage(array)
print(str(result) + '.0')



In this code, the computeAverage function iterates over each element in the array and adds it to a sum variable. After iterating through all the elements, it calculates the average by dividing the sum by the length of the array. Finally, the main program reads the size of the array, fills it with user input values, calls the computeAverage function, and prints the result.

User Deepak S
by
8.0k points