Welcome

Hi! This blog is for beginners of c++ and java .You can check and pick codes for your programming assignments. This will also help you to understand the concepts of these programming languages. Its all about programming.

Templates practice (Averege Of three arrays)

Wednesday, May 25, 2011

#include<iostream>
using namespace std;
template<class T>
T calc(T a[],int size)
{
 T sum=0,res=0;
 for(int i=0;i<size;i++)
 {
  sum=sum+a[i];
 }
 res=(sum/size);
 return res;
}

void main()
{
 int a[]={2,7,6,8,7,8};
 cout<<"The average is "<<calc(a,6)<<endl;
 float b[]={2.09,7.14,8.7,7.9,8.2};
 cout<<"The average is "<<calc(b,5)<<endl;
 long c[]={200, 600, 8010, 950, 7890, 6788};
 cout<<"The average is "<<calc(c,6)<<endl;

}