The following code generates a list arr with 100 items.
import random
random.seed(0)
arr = [ random.randint(1,100) for _ in range(100) ]
Write a program that computes the sum of squares of all the numbers in the list arr
Submit both the code and result (sum of squares of all the numbers in arr)
Implement the program as follows:
Program: Language - Python
Note: Please double-check the indentation using the screenshot provided as a reference
import random # import random module
random.seed(0) # Initialize random module for generating random values
arr = [ random.randint(1,100) for _ in range(100) ] # create a list, arr and initialize it with 100 random values
print("List arr :")
print(arr) # print list, arr
sum_of_squares = 0 # initialize sum_of_squares to 0
for value in arr: # for each value in arr list
sum_of_squares += value*value # calculate square of value as value*value and add it to sum_of_squares
print("Sum of squares = " + str(sum_of_squares)) # print sum_of_squares
Screenshot:
Output:
Please don't forget to give a Thumbs Up.
Get Answers For Free
Most questions answered within 1 hours.