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 that reads in a table of numbers and finds out the average...
Write a C++ program that reads in a table of numbers and finds out the average of each row and column. This program should first take two numbers as number of rows and columns as input from the user
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
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
Write a program in C++ to read 10 numbers from keyboard and find their sum, average,...
Write a program in C++ to read 10 numbers from keyboard and find their sum, average, maximum and minimum
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
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT