Here we will find out sum of all the elements of array using function template whether array is of integer type or float type.
#include<conio.h>
#include<iostream>
using namespace std;
template <class T>
void sum(T arr[],int n)
{
T sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
cout<<"\nThe sum of all the elements of the array is = "<<sum;
}
int main()
{
int n;
cout<<"Enter total number of elments in the array: ";
cin>>n;
int arr[n];
float arrf[n];
cout<<"\n Enter integer values in the array:\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
sum(arr,n);
cout<<"\nEnter flaoting point values:\n";
for(int i=0;i<n;i++)
{
cin>>arrf[i];
}
sum(arrf,n);
getch();
return 0;
}
Naveen Dengani
Shashank Chauhan
No comments:
Post a Comment