自考新教材-p352_3
Posted duanqibo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自考新教材-p352_3相关的知识,希望对你有一定的参考价值。
源程序:
#include <iostream>
#define N 10
using namespace std;
template <typename T>
T sum_array(T a[], int n)
{
int i,size;
T sum=0;
cout << "您想求数组前几项的和,请输入:";
cin >> size;
for (i = 0; i < size; i++)
{
sum = sum + a[i];
}
return sum;
}
int main()
{
int num[N] = {1,3,5,7,9,2,4,6,8,0};
int result = sum_array(num,N);
cout << result << endl;
cout << endl;
double num1[N] = { 1.1,3.3,5.5,7.7,9.9,2.2,4.4,6.6,8.8,0.0 };
double result1 = sum_array(num1, N);
cout << result1 << endl;
cout << endl;
char num2[N] = { ‘a‘,‘c‘,‘e‘,‘f‘,‘h‘,‘m‘,‘p‘,‘b‘,‘j‘,‘d‘ };
int result2 = sum_array(num2, N);
cout << result2 << endl;
system("pause");
return 1;
}
运行结果:
以上是关于自考新教材-p352_3的主要内容,如果未能解决你的问题,请参考以下文章