美文网首页
【MAC 上学习 C++】Day 60-4. 6-4 求自定类型

【MAC 上学习 C++】Day 60-4. 6-4 求自定类型

作者: RaRasa | 来源:发表于2019-10-23 20:10 被阅读0次

    6-4 求自定类型元素的平均 (10 分)

    1. 题目摘自

    https://pintia.cn/problem-sets/14/problems/736

    2. 题目内容

    本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType。

    函数接口定义:

    ElementType Average( ElementType S[], int N );
    其中给定集合元素存放在数组S[]中,正整数N是数组元素个数。该函数须返回N个S[]元素的平均值,其值也必须是ElementType类型。

    输入样例:

    3
    12.3 34 -5

    输出样例:

    13.77

    3. 源码参考
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    #define MAXN 10
    typedef float ElementType;
    
    ElementType Average( ElementType S[], int N );
    
    int main ()
    {
        ElementType S[MAXN];
        int N, i;
    
        cin >> N;
    
        for ( i=0; i<N; i++ )
        {
          cin >> S[i];
        }
    
        cout << fixed << setprecision(2) << Average(S, N) << endl;
    
        return 0;
    }
    
    ElementType Average( ElementType S[], int N )
    {
      ElementType s;
    
      s = 0;
      for(int i = 0; i < N; i++)
      {
        s += S[i];
      }
    
      return s / N;
    }
    

    相关文章

      网友评论

          本文标题:【MAC 上学习 C++】Day 60-4. 6-4 求自定类型

          本文链接:https://www.haomeiwen.com/subject/pqtbmctx.html