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 scanf("%f", &S[i]); printf("%.2f\\n", Average(S, N)); return 0; } /* 你的代碼將被嵌在這裡 */ 輸入樣例: 3 12.3 34 -5 輸出樣例: 13.77
閱讀更多 叫我千千 的文章