A Problem Solved—Computing Deviations

In this lesson, we will look at an example of array processing. We will read some user-supplied numbers and compute the deviation of each number from the average of all the numbers read.

We'll cover the following

Problem statement

We wrote some code in this chapter’s first lesson that read numbers from the user and computed their average value. We will now modify that code so that it also computes the deviation of each number from the average.

We begin by defining an array numbers that is just large enough to accommodate the numbers we plan to read as data. As we read those numbers, we will save them in the array as well as accumulate their sum. Afterward, we’ll compute and display the deviation of each number from their average.

The program

Run the following program using the five numbers 1.2, 2.3, 3.4, 4.5, and 5.6 as data. Note the loss of precision in the results due to the way a computer represents real numbers.

Get hands-on with 1200+ tech skills courses.