Question

The following code generates a list arr with 100 items. import random random.seed(0) arr = [...

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)

Homework Answers

Answer #1

Implement the program as follows:

  1. import random module
  2. Initialize random module for generating random values
  3. create a list, arr and initialize it with 100 random values
  4. print list, arr
  5. initialize sum_of_squares to 0
  6. for each value in arr list
  7. calculate square of value as value*value and add it to sum_of_squares
  8. print sum_of_squares

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given the following code snippet, what is this program calculating? for (int i = 0; i...
Given the following code snippet, what is this program calculating? for (int i = 0; i < n; i++) { sum += arr[i]; } sum /= n;
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
The function named myRandomNum that returns a random integer in the range [1, 100] is defined...
The function named myRandomNum that returns a random integer in the range [1, 100] is defined as follows: int myRandomNum () { return rand()%100 + 1; } Now, write a program (code fragment) that repeatedly generates and prints random integers in the range [1, 100]. The program must stop when the absolute value of the difference between successive printed values is less than 5. Two sample output is given to help you understand the problem: 1. The program can possibly...
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order....
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'. def sort_in_place(list_num): for i in range(?): for j in range(?): if ?: temp = list_num[j] list_num[j] = list_num[i] list_num[i] = temp my_list = [23,1,45,20,13,-34] sort_in_place(my_list) print(my_list) Modify the three lines of code in the program below so that the output is [-34, 1, 13, 20, 23, 45]
I'm working with the below dictionaries and list: stock = {"banana": 6, "apple": 0, "orange": 32,...
I'm working with the below dictionaries and list: stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15} prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3} groceries = [‘apple’, ‘banana’, ‘pear’] I need to write code to sum the total number in stock of the items in the groceries list. The second part is to write code to print the total value in stock of all items. The program can iterate over the stock dictionary and for each...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
6) Write the Python Code which can answer the following questions (20 pts): a)Consider a program...
6) Write the Python Code which can answer the following questions (20 pts): a)Consider a program which contains a list of numbers from 1 to 10that are stored into a variable a. Create a program that will allow you to exchange the elements from variable a at position 0 and 7from within this list. b)Create a program that will access the last 5 elements of the variable a from step a.)
Write an R code to print out all even numbers from the following numbers list in...
Write an R code to print out all even numbers from the following numbers list in the same order they are received. Write the code so it does not print any numbers that come after 83. numbers = [951, 40, 84, 51, 60, 69, 48, 19, 61, 85, 98, 50, 72, 47, 44, 61, 83, 65, 41, 51, 63, 61, 65, 75, 21, 30, 84, 92, 23]
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following questions using the list below. You can record your answers in the essay textbox. data = [5,3,7] A. Write code to replace the value in the 0 position with the number 8. B. Add the value 10 to the end of the list. C. Insert the value 22 after position 1 in the list. D. Remove the value at position 2. E. Sort the...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT