Ask Question
16 July, 07:13

9.6 edhesive question 2

Add a method named sumArray to your last program that takes an array as a parameter and returns the sum of its values.

Sample Run

How many values to add to the array:

8

[17, 99, 54, 88, 55, 47, 11, 97]

Total: 468

my code so far is

import random

def buildArray (a, b):

for i in range (b):

random_num=random. randint (10,99)

a. append (random_num)

return a

values=int (input ("How many values to add to the array: "))

array = []

print (buildArray (array, values))

+5
Answers (1)
  1. 16 July, 10:47
    0
    import random

    def buildArray (a, b):

    for i in range (b):

    random_num = random. randint (10, 99)

    a. append (random_num)

    return a

    def sumArray (arr):

    total = 0

    for e in arr:

    total + = e

    return total

    values = int (input ("How many values to add to the array: "))

    array = []

    print (buildArray (array, values))

    print ("Total: " + str (sumArray (array)))

    Explanation:

    *Added parts are highlighted.

    Create a method called sumArray that takes one parameter, arr

    Initialize total as 0

    Create a for loop iterates through arr

    Add each value in the arr to the total

    When the loop is done, return the total

    After you build your array, call your method, sumArray, pass the created array as parameter and print the total
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “9.6 edhesive question 2 Add a method named sumArray to your last program that takes an array as a parameter and returns the sum of its ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers