1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#include <iostream>
using std::cout;
using std::endl;
#include <cstdarg>
// function prototype(s)
int avg(int, ...);
double davg(double, ...); // added double function
int avgx(int&, int&, int, ...);
int main()
{
cout << "The average of 5 test scores is " << avg(5, 81, 92, 73, 84, 95) << endl;
cout << "The average of 5 double scores is " << davg(5, 71.1, 22.3, 63.4, 94.3, 95.3) << endl;
int a, aMax, aMin;
a = avgx(aMax, aMin, 8, 51, 26, 12, 65, 25);
}
// return average of a variable length list of integers
int avg(int n , ...) // "n" is the numbers in the list "..." is the list
{
va_list list; // assign the name "list" to a variable length list of integers
va_start(list, n); // tell c__ that the list begins AFTER the argument "n"
int num; // store the numberes from the list in "num" as they are "read"
// create the total of "n" numbers in the list
int total = 0; // track the total of the numbers in the list
for (int i = 0; i < n; i ++)
{
num = va_arg(list, int); // set num equal to the next number in the list, as an int
total = total + num; // increment the total
}
va_end(list); // close the list  REQUIRED
// compute and return the average
return total / n;
}
double davg(double d, ...) // "d" is the numers in the list
{
va_list list;
va_start(list, d);
double num;
double total = 0;
for (int i = 0; i < d; i ++)
{
num = va_arg(list, double);
total = total + num;
}
va_end(list); // close the list
return total / d;
}
// returns average, and passes max and min back through argument list
int avg(int& mx, int& mn, int n, ...) // n is the numbers of numbers in the list
{
va_list list; // assign the name "list" to the variable length list of integers
va_start(list, n); // tell c++ that the list begins AFTER the argument "n"
int num; // store the numbers from the list in "num" as they are "read"
// create the total of "n" numbers in the list
int total = 0; // track the total of the numbers in the list
for (int i = 0; i < n; i ++)
{
num = va_arg(list, int); // set num equal to the next number in the list , as an int
if ((i == 0)  (mn > num)) // update min value
mn = num;
if ((i == 0)  (mx < num)) // update max value
mx = num;
total = total + num; // increment the total
}
va_end(list); // close the list  REQUIRED
// compute and return the average
return total / n;
}
 