Saturday, September 27, 2014

Create a function called sum () that returns the sum of the elements of an array. Make this function into a template so it will work with any numerical type. Write a main program that applies this function to data of various types.

#include
#include
template
T sum(T *a,int n)
{
            T s=0;
            for(int i=0;i
            {
                        s=s+a[i];
            }
            return s;
}
void main()
{
            clrscr();
            int n;
            double a[20];
            cout<<"Enter no. of elements:";
            cin>>n;
            cout<<"\nEnter the elements:";
            for(int i=0;i
            {
                   cin>>a[i];
            }
            cout<<"\nSum: "<
            getch();

}

No comments:

Post a Comment