Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A program often checks data before using it. A good prompt frequently includes a

ID: 3619480 • Letter: A

Question

A program often checks data before using it. A good prompt frequently includes a limit specification, and if the data is not reasonable, the program can indicate that by simply repeating the prompt, thereby asking for the same data again.



Suppose you want to compute the average weight of a group of items. To do this, initialize cumulativeWeight to 0, count to 0, and more to ‘y’. Then enter a loop that repeats while more equals ‘y’. Just before the end of this loop include a prompt asking “more? (y/n): ” and input the user’s answer. Inside this (outer) loop, increment count and print it out to tell the user the number of the next item. Then preset the input variable to some unacceptable value. For example, set weight to -1, and enter a second (inner) loop that repeats while weight is less than or equal to 0. In the inner loop, print “Enter kg weight greater than zero: ” and then input a value for the weight. This inner loop should automatically repeat until the input is valid. After this inner loop terminates, add the (valid) input to cumulativeWeight. After an ‘n’ input for more terminates the outer loop, compute the average by setting weight to cumulativeWeight / count, and print it out. Here is a typical interactive display:





Sample session:



Item number 1:

Enter kg weight (greater than zero): 1.23

more? (y/n): y

Item number 2:

Enter kg weight (greater than zero): 2.78

more? (y/n): y

Item number 3:

Enter kg weight (greater than zero): -0.95

Enter kg weight (greater than zero): 0.95

more? (y/n): n

Average weight = 1.6533333333333333 kg



Use pseudocode to write the algorithm for a program that is able to generate the above display.

Explanation / Answer

Algorithm: Set cumlativeWeight = 0 set count = 0 set more = 'y' Create a scanner called in while more equals y print item number
ask user to enter weight
add entered weight to cumlative weight
ask for more
put answer in more variable
increase count by 1 print item number
ask user to enter weight
add entered weight to cumlative weight
ask for more
put answer in more variable
increase count by 1 print out the average weight by dividing cumlativeweight by count Actual code:
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote