ex14_1.cpp

来自「本课程主要介绍面向对象程序设计的方法和c++语言的基本概念。以c++语言中的面向」· C++ 代码 · 共 28 行

CPP
28
字号
// ex14_1.cpp
// template used for function that averages array
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
template <class atype>             //function template
atype avg(atype* array, int size)
	{
	atype total = 0;
	for(int j=0; j<size; j++)       //average the array
		total += array[j];
	return (atype)total/size;
	}
////////////////////////////////////////////////////////////////
int intArray[] =       {1, 3, 5, 9, 11, 13};
long longArray[] =     {1, 3, 5, 9, 11, 13};
double doubleArray[] = {1.0, 3.0, 5.0, 9.0, 11.0, 13.0};
char charArray[] =     {1, 3, 5, 9, 11, 13};

int main()
	{
	cout << "\navg(intArray)=" << avg(intArray, 6);
	cout << "\navg(longArray)=" << avg(longArray, 6);
	cout << "\navg(doubleArray)=" << avg(doubleArray, 6);
	cout << "\navg(charArray)=" << (int)avg(charArray, 6) << endl;
   return 0;
	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?