Write a function template that gets an array of generic type

Question

Write a function template that gets an array of generic type T as well as the number of items within the array. Calculates the variance of the items within the array and returns it.

The user will first give an intergeritemCount value (int) and that many integer values. Then the user will give a doubleitemCount value and that many double values.

Then, the program will use the function template to calculate the variances of both arrays and print them out as first the variance of the int array, then the variance of the double array.

Variance of an array with size N, values are given as x and arithmetic mran as can be calculated as:
var=

Requirements:
You must define and use a function template that takes an array of generic type T and an integer which defines the size of the array.

Getting all the user input, calling the appropriate version of the function via the template and printing the output should be done in the main() function.

Note: The size of each array will be At Most 1000. You do not need to check this.
input: 3 5 6 7 6 1,2,3,4,5,6,2,0,6
output:0.666667 3.86139
input: 6 5 6 7 2 3 4 6 4 0,0,2,0,6,0,4
output 581.167 0.05
input: 8 356 12 7 0 0 3 6 5 -5,6,-2,2,-1,1,-0.8 6,54
Output: 339,625 15.7038

Summary

Here in the given question, we have to write a function template that gets an array of generic types. Where we have already given a functions and methods name. So with that, we also have requirements and Notes.

Explanation

Templates in the C++ languages are the functions that allow functions and classes to operate with the generic data types. So This means that functions and classes can behold the different data types without being rewritten for the different data types. Such that we will pass this as a parameter such that we don’t have to write the same code at the different data types.
SO here in c++, there are in total two keywords to declare the template they are ‘template’ and ‘typename’. We created a template for this program. In the main method, there are variables with data type int and double. After that, there are many cout statements that will present the output statements on the screen.

Code

#include<iostream>
#include<cmath>
using namespace std;

template <typename T>
void variance(T arr[], T num);

int main()
{	int intItemCount, intValues[1000];
    double doubleItemCount, doubValues[1000];
        
    //input information for integers	
    cout<<"\nEnter integer item count: ";
    cin>>intItemCount;
    cout<<"Enter values:\n";
    for(int i=0; i<intItemCount; i++)
        cin>>intValues[i];
        
    //input information for double
    cout<<"\nEnter double item count: ";
    cin>>doubleItemCount;
    cout<<"Enter values:\n";
    for(int i=0; i<doubleItemCount; i++)
        cin>>doubValues[i];
        
    //function call
    variance(intValues,intItemCount);
    variance(doubValues,doubleItemCount);
return 0;
}

//variance function template
template <typename T>
void variance(T arr[], T num)
{	double var=0;
    double mean=0;
    
    for(int i=0; i<num; i++)
        mean+=arr[i];
    mean=mean/num;
    
    for(int i=0; i<num; i++)
    {	var+=pow(arr[i]-mean,2);
    }
    var=var/num;
    cout<<"\nVariance is: "<<var;
}

Output

array of generic type

 

Also read, Given the below defined UML class diagram.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *