Complete the program below by writing functions average and deviation. The program's purpose is to input a list of real numbers, compute their average, and display a table with each number and its deviation from the average. For example, if the data were 4.00, 12.00, 7.00, and 5.00 (average = 7.00), the deviation list would be -3.00, 5.00, 0.00, and -2.00.
#include
#include
using namespace std;
double average( );
void deviation( );
const int MAX_SIZE = 20;
int main()
{
double mean;
double nums[MAX_SIZE];
double devFromMean[MAX_SIZE];
int numsSize;
cout << "Enter the number of elements in the list => ";
cin >> numsSize;
cout << "Enter the elements of the list separated by a space => ";
for (int i = 0; i < numsSize; ++i)
cin >> nums[i];
mean = average( nums, numsSize );
cout << "The mean is " << mean << endl;
deviation( devFromMean, nums, numsSize, mean );
for (int i = 0; i < numsSize; ++i)
cout << setw( 10 ) << nums[i] << setw( 10 ) << devFromMean[i]
<< endl;
return 0;
}
// Returns the average of the first n elements of list
double average( )
// Fills each of the first n elements of devList with the deviation from
// givenVal of the corresponding element of list.
void deviation(
// Prototypes
double average( const double [], int );
void deviation( double [], const double [], int, double );
// Function definitions
//
// Returns the average of the first n elements of list
double average( const double list[], int n )
{
double sum = 0;
for (int i = 0; i < n; ++i )
sum += list[i];
return (sum / n);
}
// Fills each of the first n elements of devList with the deviation from
// givenVal of the corresponding element of list.
void deviation( double devList[], const double list[], int n,
double givenVal )
{
for (int i = 0; i < n; ++i)
devList[i] = list[i] - givenVal;
}
Computer Science & Information Technology
You might also like to view...
Of the classes below, the one that is most likely to be declared abstract is _________________.
a) Bat b) Squirrel c) Animal d) Iguana e) Parrot
Computer Science & Information Technology
To print the current Web page, select the _____ menu, then select Print.
A. File B. View C. Output D. Edit
Computer Science & Information Technology
An icon is a small image that represents a command, file, or another window
Indicate whether the statement is true or false
Computer Science & Information Technology
The JVM checks the values of subscripts before using them and throws an exception if they are out of bounds.
Answer the following statement true (T) or false (F)
Computer Science & Information Technology