Question

In: Computer Science

Part 1: Write a program that finds the sum and average of a series of numbers...

Part 1: Write a program that finds the sum and average of a series of numbers entered by the user. The program will first ask the user how many numbers there are. It then prompts the user for each of the numbers in turn and prints out the sum, average, the list of number from the user. Note: the average should always be a float, even if the user inputs are all ints.

Part 2: Same as part 1 except now the numbers are to be read in from a file.
File contains a single line of numbers delimited by comma. Example: 8, 39, 43, 1, 2, 3

Part 3: Part 2 but modular ( i.e. using functions).
Assume the file name is passed to the function as a parameter. The function then opens the file, reads in the numbers, then computes and returns the sum and average of the numbers.

Solutions

Expert Solution

Part 1:

n=int(input('How many numbers there are??:'))
s=0
for i in range(n):
    val=int(input('Enter number:'))
    s=s+val
average=float(s/n)
print('Sum is:',s)
print('Average is:',average)

OUTPUT

Part2:

s=0
c=0
with open('file.txt','r') as f:
    for line in f:
        num=line.split(',')
for i in num:
    s=s+int(i)
    c=c+1
average=float(s/c)
print('Sum is:',s)
print('Average is:',average)

file.txt

OUTPUT

part3:

def fun(fname):
    s=0
    c=0  #used to count numbers present in file.
    f=open(fname)
    for line in f:
        num=line.split(',') # it splits the line by , ( since numbers are separated by comma[,])
    for i in num:       #num contains list of numbers
        s=s+int(i)
        c=c+1
    average=float(s/c)
    return s,average
s,ave=fun('file.txt')
print('Sum is:',s)
print('Average is:',ave)

OUTPUT


Related Solutions

Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a complete program to sum the following series: (hint: use the for loop) 1 /2...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2 + 1/ 4 + 1 /6 + ⋯ + 1 /100 PS: use C++ language please
1.write a small program using a loop to add a series of numbers 2.write a function...
1.write a small program using a loop to add a series of numbers 2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes In python and doesn't have to be long. just long enough to do what it says. Thank you.
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT