47.5k views
4 votes
Write a java program called DistanceFromAverage which allows a user to enter up to 15 double values. The user should enter 99999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise, display a count of the numbers entered, the arithmetic average of the numbers, and each entered value and its distance from the average.

An example of the program is show below:

Enter a numeric value or 99999 to quit >> 12.2

Enter next numeric value or 99999 to quit >> 11

Enter next numeric value or 99999 to quit >> 18.2

Enter next numeric value or 99999 to quit >> .5

Enter next numeric value or 99999 to quit >> 99999

You entered 4 numbers and their average is 10.475

12.2 is 1.7249999999999996 away from the average

11.0 is 0.5250000000000004 away from the average

18.2 is 7.725 away from the average

0.5 is -9.975 away from the average

1 Answer

0 votes

Final answer:

To solve this problem, you can use a for loop to iterate up to 15 times and prompt the user to enter a number each time. After the loop, calculate the count of numbers entered, the average of these numbers, and the distance of each number from the average.

Step-by-step explanation:

To solve this problem, you can use a for loop to iterate up to 15 times and prompt the user to enter a number each time. Inside the loop, store each entered number in an array. If the user enters 99999, break out of the loop. After the loop, check if the array is empty. If it is, display an error message. Otherwise, calculate the count of numbers entered and the average of these numbers. Then, iterate over the array again and calculate the distance of each number from the average. Finally, display all the calculated values.

User Ada Lovelace
by
8.7k points