Averaging custom type elements

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43912065/article/details/102731930

This problem required to achieve a function, find a set of N elements S [] is an average value, wherein the set of types of elements is custom ElementType.

Function interface definition:
the ElementType Average (the ElementType S [], int N);
wherein the given set stored in an array element S [], the positive integer N is the number of array elements. This function shall return the N S [average] element, its value must be the type ElementType.

Referee test sample program:
#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;

}

/ * Your code here will be embedded * /
Input Sample:
3
12.3 34-5
Output Sample:
13.77

ElementType Average(ElementType s[],int N)
{
ElementType sum=0;
int i;
for(i=0;i<N;i++)
{
sum+=s[i];
}
return sum/N;
}

Guess you like

Origin blog.csdn.net/weixin_43912065/article/details/102731930