The purpose of this lab is to code template functions and specific-instance functions.
You will be coding three functions. One function will be purely a template
function which calculates the average of an array of values. The other two
functions, both called printArray
, will print an array of values.
One will be a specific-instance function to print out doubles with 3 decimal
places of precision. The other will be a template function.
The function specifications are:
calcAverage
: It will take a template array and an
integer for the size. It will return a double containing the calculated
average. The body of the function calculates the sum of all of the
elements in the array. It then returns the sum divided by the size. Make
sure to typecast the size to a double so that the function will always
do double division.
printArray
: It will take a template array and an
integer for size. It is a void function. The body of the function will
loop over the array and print each element seperated by a space to the
screen.
printArray
: It will take a double array
and an integer for size. It is a void function. The body of the function
will loop over the array and print each element with 3 decimal places of
precision. Each element will also be seperated by a space.
int main() { int iArr[] = {4, 7, 19, 71, 55}; char cArr[] = {'a', 'y', '%', '!', 'T'}; double dArr[] = {8.3, 7.2, 19.65, 2.5, 15.27}; printArray(iArr, 5); cout << "The average is " << calcAverage(iArr, 5) << endl; printArray(dArr, 5); cout << "The average is " << calcAverage(dArr, 5) << endl; printArray(cArr, 5); return 0; }If you have done your functions correctly, the double array should print out with 3 decimal places of precision while the integer and character arrays will just print out with just spaces. Both
calcAverage
functions should
calculate the average of the two numeric arrays using double division.
Email your completed code to me.