You are to use an array of doubles to compute the average of a sequence of numbers and the standard deviation of the numbers. Based on these value you will print for each of the values in the array: the array index, the value, the deviation from the average and the deviation divided by the standard deviation.


Using Arrays

You will use an array of 100 doubles in your code. The array is named x and the 100 values are x[0], x[1], ... x[99]. Of course you will be using the array elements in loops with an index variable i, so you would use x[i] to refer to the current array element. You can use an array element much like a simple variable:

x[i] = 1.0;
x[i] = x[i] * 2.0
cin >> x[i];


Average

The average of a sequence of numbers is computed by adding the numbers and then dividing by the number of values. For example, let's suppose we have 5 numbers: 1, 2, 3, 4, and 5. The sum of these numbers is 15 and the average is 15/5 or 3.

Standard Deviation

The standard deviation is computed by first subtracting the average from each number. This gives us the following deviations:

1.0 - 3.0 = -2.0
2.0 - 3.0 = -1.0
3.0 - 3.0 = 0.0
4.0 - 3.0 = 1.0
5.0 - 3.0 = 2.0

Now we might be tempted to compute the average of the deviations, but that will always be 0. Instead we can square the deviations and compute the average squared deviation:

-2.0 * -2.0 = 4.0
-1.0 * -1.0 = 1.0
0.0 * 0.0 = 0.0
1.0 * 1.0 = 1.0
2.0 * 2.0 = 4.0

The sum of the squared deviations is 10.0 and the average is 2. Taking the square root of this average squared deviation yields the standard deviation - sqrt(2.0) = 1.414.

Normalized Deviations

Now if we go back through the deviations from before, we can normalize them by dividing by the standard deviation:

1.0 - 3.0 = -2.0 -2.0 / 1.414 = -1.414
2.0 - 3.0 = -1.0 -1.0 / 1.414 = -0.707
3.0 - 3.0 = 0.0 0.0 / 1.414 = 0.0
4.0 - 3.0 = 1.0 1.0 / 1.414 = 0.707
5.0 - 3.0 = 2.0 2.0 / 1.414 = 1.414

Using normalized deviations is fairly handy, since for any collection of data the normalized values will mostly be between -2 and +2. This gives a way of viewing the data which is scaled to match the data. If this were a collection of grades for a class, a professor might assign grades based on the normalized deviation for each student. A 0.0 would be average and might be a C. Perhaps 0.8 would be required for a B and 1.6 for an A.

Your starting code

You should copy the code below and fill in the missing pieces. The organization has been done for you and all that remains is to fill in several lines of code which are well-commented.

#include <iostream>
#include <cmath>
#include <cassert>

using namespace std;

const int MAX = 100;

//
// This function should read data into the array x until
// an attempt to read from cin fails.
//
// The return value of the function should be the number
// of values successfully read.
//
int read_data ( double x[], int max )
{
int i;

for ( i = 0; i < max; i++ ) {
//
// Attempt to read a value into x[i]
//

//
// If the read fails, return i
//

}
return max;
}

//
// This function computes the average of n values from
// the array x.
//
// The return value is the average.
//
double average ( double x[], int n )
{
double sum = 0.0;
int i;

assert ( n >= 1 && n <= MAX );
for ( i = 0; i < n; i++ ) {
//
// Add x[i] to sum
//
}
//
// Return the sum divided by n
//
}

//
// This function computes the standard deviation n
// values from the array x.
//
// The return value is the standard deviation.
//
double standard_deviation ( double x[], int n, double avg )
{
double sum = 0.0;
double dev;
int i;

assert ( n >= 1 && n <= MAX );
for ( i = 0; i < n; i++ ) {
//
// Set dev to be x[i] - avg
//

//
// Add dev*dev to sum
//
}

//
// At this point sum is the sum of all the deviations squared.
// We can get the average squared deviation by dividing sum by n.
// The standard deviation is the square root of the average
// deviation.
//
// Return the standard deviation.
//
}

//
// This function will issue a report detailing how each data
// value deviates from the average. In the loop in the function
// you need to print for each value of i: i, x[i], the deviation
// of x[i] from the average and the normalized deviation.
//
void report_deviations ( double x[], int n, double avg, double st_dev )
{
double dev, normalized_dev;
int i;

assert ( n >= 1 && n <= MAX );
assert ( st_dev > 0.0 );
cout << "average: " << avg << endl;
cout << "standard deviation: " << st_dev << endl;

for ( i = 0; i < n; i++ ) {
//
// Set dev to be x[i] - avg
//

//
// Set normalized_dev to be dev / std_dev;
//

//
// Print i, x[i], dev and normalized_dev
//
}
}

int main()
{
double x[MAX];
double avg, st_dev;
int n;

n = read_data ( x, MAX );
assert ( n >= 1 && n <= MAX );
avg = average ( x, n );
st_dev = standard_deviation ( x, n, avg );
report_deviations ( x, n, avg, st_dev );

return 0;
}

The instructions are very clear. I hope you have started coding (starting from the template supplied).

If you have any questions, please post.

To compute the average and standard deviation of a sequence of numbers using an array of doubles, you will need to fill in the missing code sections in the provided code.

1. read_data() function:
This function is responsible for reading data into the array `x` until an attempt to read from `cin` fails. You need to loop through the array indices and use `cin` to read values into `x[i]`. If the read fails, you should return the current index `i`. Here's how you can implement this function:

```cpp
int read_data(double x[], int max) {
int i;

for (i = 0; i < max; i++) {
if (!(cin >> x[i])) {
return i;
}
}
return max;
}
```

2. average() function:
This function computes the average of `n` values from the array `x`. You need to iterate through the array indices and add each `x[i]` to the `sum` variable. Finally, return `sum` divided by `n`. Here's how you can implement this function:

```cpp
double average(double x[], int n) {
double sum = 0.0;
int i;

assert(n >= 1 && n <= MAX);
for (i = 0; i < n; i++) {
sum += x[i];
}
return sum / n;
}
```

3. standard_deviation() function:
This function computes the standard deviation of `n` values from the array `x`. You need to iterate through the array indices, subtract the `avg` from each `x[i]` to get the `dev` value, and add `dev*dev` to the `sum` variable. Finally, return the square root of `sum/n`. Here's how you can implement this function:

```cpp
double standard_deviation(double x[], int n, double avg) {
double sum = 0.0;
double dev;
int i;

assert(n >= 1 && n <= MAX);
for (i = 0; i < n; i++) {
dev = x[i] - avg;
sum += (dev * dev);
}

return sqrt(sum / n);
}
```

4. report_deviations() function:
This function will print a report detailing how each data value deviates from the average. You need to iterate through the array indices and calculate the `dev` (deviation) by subtracting `avg` from each `x[i]`. Then, you need to calculate the `normalized_dev` by dividing `dev` by `st_dev` (standard deviation). Finally, print the index `i`, `x[i]`, `dev`, and `normalized_dev` for each value. Here's how you can implement this function:

```cpp
void report_deviations(double x[], int n, double avg, double st_dev) {
double dev, normalized_dev;
int i;

assert(n >= 1 && n <= MAX);
assert(st_dev > 0.0);

cout << "average: " << avg << endl;
cout << "standard deviation: " << st_dev << endl;

for (i = 0; i < n; i++) {
dev = x[i] - avg;
normalized_dev = dev / st_dev;

cout << "Index: " << i << ", Value: " << x[i]
<< ", Deviation: " << dev
<< ", Normalized Deviation: " << normalized_dev << endl;
}
}
```

After completing the implementation, you can run the program and it will read data into the array `x`, compute the average and standard deviation, and print the report with deviations for each value in the array.