Question

The following is the beginning of an algorithm that accepts an arbitrary collection of numbers from...

The following is the beginning of an algorithm that accepts an arbitrary collection of numbers from the user (who signifies the end of the input with the value -1)(it's Python):

values = []
print('Enter positive values (-1 to end):')
num = int(input('> '))

(a) Complete the program:

  • Collect the data from the user in the list values, using a loop
  • Once the loop completes, print the total sum and the average
  • Take care to handle the case where the user enters -1 as the first value
    • Print "n/a" for the average in this case

(b) What is the special value (-1) known as, for this type of algorithm?

The following shows the output from a couple of runs of the completed program:

Enter positive values (-1 to end):
> 4
> 12
> 5
> 9
> -1
Total sum: 30
Average: 7.5
Enter positive values (-1 to end):
> -1
Total sum: 0
Average: n/a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

values = []

print('Enter positive values (-1 to end):')

num = input('> ')

while num.upper() != '-1' :

values.append(int(num))

num = (input('> '))

print("Total sum: ", sum(values))

if len(values)==0:

print("Average: n/a")

else:

print("Average:", sum(values)/len(values))

=========================================================================
SEE OUTPUT


Files main.py num = main.py saved 1 values = [] 2 print( Enter positive values (-1 to end):) 3 input(> ) 4 while num. upp

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
The following is the beginning of an algorithm that accepts an arbitrary collection of numbers from...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum...

    2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum of both of the integers and all the numbers in between Write a Marie program that determines the largest of a series of positive integers provided by a user. The user will enter a -1 when she is finished. Up to 10 numbers will be provided by the user. Write a Marie program that accepts two positive integers, multiples them by repeated addition, and...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • 1.The following pseudocode algorithm is used to calculate the average of two numbers. Perform a desk...

    1.The following pseudocode algorithm is used to calculate the average of two numbers. Perform a desk check on this algorithm assuming the firstValue is 12.5 and the secondValue is 7.5. You should use a table to show your answer. L1: Program AverageCalculator; L2: Data firstValue as float; L3: Data SecondValue as float; L4: Data average as float; L5: Output “Enter the first Number”; L6: Input firstValue; L7: Output “Enter the second Number”; L8: Input secondValue; L9: average = (firstValue+secondValue)/2; L10:...

  • Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...

    Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of Numbers/C++ program Write a C ++ program that asks the user for a positive integer value by prompting "Enter a positive integer number: ", read in that number, then use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2,3, 4…50....

  • In C++ This program will read a group of positive numbers from three files ( not...

    In C++ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the scores in the order that they were input, showing each number and what percentage it is above or below the average for each file. Note: Finding the average doesn’t require an array. Finding...

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

  • Write a program that will loop ten times. In each iteration prompt user to enter an...

    Write a program that will loop ten times. In each iteration prompt user to enter an integer between -50&50. Print the input, keep a running sum of the inputs, and track the minimun and maximum numbers input. After the loop is complete, minimum number entered, maximum number entered, sum of all numberes entered, and average of all numbers entered. Format all output. Before running the loop, print the label "Input values: ". Within the loop, print value entered by user...

  • ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real...

    ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...

  • C++ Programming Help Please! NOTE: Please READ All Steps very carefully. DO #10! (10 is based...

    C++ Programming Help Please! NOTE: Please READ All Steps very carefully. DO #10! (10 is based off of 9). Program Info:   #9. Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once...

  • and then print their average. Write a C program that accepts a string of text from...

    and then print their average. Write a C program that accepts a string of text from the user and prints back the string without any of the vowels. For example, if the user entered "Hello, world", the program should print "Hll, wrld" Write a C program that accepts an integer in decimal from the user and prints the number of 1' bits in the integers binary representation. For example, if the user entered 14, the program should print 3 because...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT