6-4 求自定类型元素的平均 (10 分)
本题要求实现一个函数,求N
个集合元素S[]
的平均值,其中集合元素的类型为自定义的ElementType
。
函数接口定义:
ElementType Average( ElementType S[], int N );
其中给定集合元素存放在数组S[]
中,正整数N
是数组元素个数。该函数须返回N
个S[]
元素的平均值,其值也必须是ElementType
类型。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
typedef float ElementType;
ElementType Average( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Average(S, N));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
12.3 34 -5
输出样例:
13.77
ElementType Average( ElementType S[], int N )
{
ElementType s =0;
int i=0;
for(i=0;i<N;i++)
{
s+=S[i];
}
s=s/N;
return s;
}
原文链接: https://www.cnblogs.com/lxzbky/p/10470219.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/400206
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!