Monday, May 10, 2021

simple program using function templates (OOP) (87)

#include<iostream>

using namespace std;

template <typename y>

y average(y u[10])

{

int sum=0;

cout<<"please enter the elements in the array:"<<endl;

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

{

cin>>u[i];

}

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

{

sum=sum+u[i];

}

return sum/10;

}

int main()

{

int arrayi[10];

cout<<"call the function with int type:"<<endl;

cout<<"average is:"<<average(arrayi)<<endl;

float arrayf[10];

cout<<"call the function with float type: "<<endl;

cout<<"average is:"<<average(arrayf)<<endl;

double arrayd[10];

cout<<"call the function with double type: "<<endl;

cout<<"average is:"<<average(arrayd)<<endl;

char arrayc[10];

cout<<"call the function with char type: "<<endl;

cout<<"average is:"<<average(arrayc)<<endl;

} 

No comments:

Post a Comment