Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 13 Notes: Here’s another example of a useful template function that uses

ID: 3769992 • Letter: C

Question

Chapter 13 Notes:

Here’s another example of a useful template function that uses arrays:

template <class T>

void printarray (const T *array, const int count)

{

   for (int i = 0; i < count; i++)

        cout << array[i] << “ “;

   cout << endl;

}

This function will print any single subscripted array of any type and size. We could use is as follows . . .

int asize = 5, bsize = 7; csize = 6;

int a[assize] = {1,2,3,4,5};

double b[bsize] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};

char c[csize] = “HELLO”;

printarray(a, asize);

printarray(b, bsize);

printarray(c, csize);

Write a C++ template function that returns the average of all the elements in an array. The arguments to the function should be the array name and the size of the array.

.

Note . . .

This function does not do any printing

Also implement the "printarray" function as discussed in the Chapter 13 notes (shown above). . . This function takes a single - subscripted array of any type and size and prints the array one element per line.

Write a main program that tests these functions using arrays of 3 different types of your choice. Fill your 3 arrays with random values for testing. For each array, you will print the array, find the average, and then print out the average. Make sure you use meaningful variables names, and label your tests in your output.

Direct your output, including your name and date, to the screen.

Explanation / Answer

Here is the code for you. If you need any further refinements, just get back to me, with your expected output.

#include <iostream>
using namespace std;
template <class T>
void printarray (const T *array, const int count)
//Template function reads the array, and the size as input, and prints the elements, one in each line.
{
for (int i = 0; i < count; i++)
cout << array[i] <<endl;
cout << endl;
}

template <class T>
double averageArray (const T *array, const int count)  
//Template function reads the array, and the size as input, and returns the average of the array.
{
double sum = 0;
for (int i = 0; i < count; i++)
sum += array[i];
return sum/count;
}

int main()
{
int asize = 5, bsize = 7, csize = 6;
int a[5] = {1,2,3,4,5};       //Integer array initialization.
double b[7] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};   //Double array initialization.
char c[6] = "HELLO";           //Character array initialization.
printarray(a, asize);           //Prints the integer array.
printarray(b, bsize);           //Prints the double array.
printarray(c, csize);           //Prints the character array.
cout<<"The average of the integer array is: "<<averageArray(a, asize)<<endl;   //Prints the average of the integer array.
cout<<"The average of the double array is: "<<averageArray(b, bsize)<<endl;   //Prints the average of the double array.
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote