anonymous
2012-05-16 17:20:37 UTC
Write a function "subtract_average" to subtract the average of the numbers in an array from each element of the array. The function should call the function to compute the array average as the first step in subtracting the average. For instance, if the array contains (1:5; 3:5; 1), then the average is 2. Subtracting the average changes the array elements to (0:5; 1:5; 1) whose average is 0. Function "subtract_average" should take two parameters, the array, and the number of elements in the array. The array parameter can either be declared as a fixed size array or as a variable size array. The array should NOT have type const. The array size should have type const. If the array parameter is a ifxed size array, the array size must be a global constant. The function modies the array and does not return any value.
In the main program, compute and print the sum and average of the array. Call the function to subtract the average from each element of the array and then print the array containing the modied list. Finally, again compute and print the average of the array which now contains the modied list. Note that the average of the modied list should always be close to zero. Because of numerical error, it may not be exactly zero."
Here's my program thus far, as you can see I just need a little help with my third function which subtracts the average of the array, and creates a new array. I understand the idea completely, but I'm not sure how to put it into the function properly (I've commented out the portions which I'm having trouble with). Thanks
#include
#include
using namespace std;
// FUNCTION PROTOTYPES
double compute_sum(int array[ ], int numElements);
double compute_average(int array[ ], int numElements);
// subtract_average(int array[ ], int numElements);
int main()
{
const int ARRAY_SIZE(100);
int array[ARRAY_SIZE];
double average(0.0);
double sum(0.0);
int numElements(0);
int x(0);
cout<< "Enter list of 100 or fewer non-zero numbers: ";
cin >> x;
numElements = 0;
while (x != 0 && numElements < ARRAY_SIZE)
{
array[numElements] = x;
numElements = numElements + 1;
cin >> x;
}
average = compute_average(array, numElements);
cout << "Average: " << average << endl;
sum = compute_sum(array, numElements);
cout << "Sum: " << sum << endl;
// modify = subtract_average(array, numElements);
// cout << "Modified array: " << modify << endl;
return 0;
}
// FUNCTION DEFINITIONS
double compute_sum(int array[], int numElements)
{
int sum(0);
for (int i=0; i
sum = sum + array[i];
};
return(sum);
}
double compute_average(int array[], int numElements)
{
int sum = compute_sum(array, numElements);
double average = double(sum)/numElements;
return(average);
}
// double subtract_average(int array[], int numElements)