Please send questions to
st10@humboldt.edu .
/*--------------------------------------------------
created by smtuttle at Tue Apr 27 22:07:02 PDT 2010
--------------------------------------------------*/
#include <iostream>
#include <cmath>
using namespace std;
/*--------------------------------------------------
Contract: sum_plus_avg : double[] int double& double& -> void
Purpose: expects an array of numbers and its size, and has two double
output parameters, and doesn't return anything, but it sets
the first output parameter to the sum of the array's
elements, and it sets the second output parameter to the
average of the array's elements
(or to 0 if the array has 0 elements...!)
Examples:
for:
const int NUM_LENGTHS = 6;
double lengths[NUM_LENGTHS] = {2, 7.5, 10, 18, 0.1, 100};
double lengths_sum;
double lengths_avg;
sum_plus_avg(lengths, NUM_LENGTHS, lengths_sum, lengths_avg);
// after this call...
lengths_sum == 137.6
lengths_avg == 22.9333
Example for boundary case:
sum_plus_avg(lengths, 0, lengths_sum, lengths_avg);
// after this call...
lengths_sum == 0.0
lengths_avg == 0.0
--------------------------------------------------*/
void sum_plus_avg(double values[], int num_values,
double& values_sum, double& values_avg)
{
double sum_so_far = 0.0;
// add up all of the values
for (int i=0; i<num_values; i++)
{
sum_so_far += values[i];
}
values_sum = sum_so_far;
// determine the average of all of the values
if (num_values == 0)
values_avg = 0.0;
else
values_avg = values_sum / num_values;
}