zoukankan      html  css  js  c++  java
  • 自考新教材-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;
    }

    运行结果:

  • 相关阅读:
    最小花费
    LOJ10090
    LOJ2436
    loj10087
    LOJ2632
    LOJ10021 Addition Chains
    LOJ10019生日蛋糕
    loj10018数的划分
    LOJ10015扩散
    loj10014数列分段二
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12267679.html
Copyright © 2011-2022 走看看