标签:ios return mamicode i++ std 结果 fine 程序 ima
源程序:
#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;
}
运行结果:
标签:ios return mamicode i++ std 结果 fine 程序 ima
原文地址:https://www.cnblogs.com/duanqibo/p/12267679.html