Basic Programming Topic Set 6-4 Find the Average of Elements of a Custom Type (10 points)

Problem Description:

Problem solving ideas:

The title is given the ElementType type, so we need to define the type of the variable is also defined according to the ElementType,

But it should be noted that when return returns, it is best to × 1.0, so as to avoid the loss of intensive reading of floating-point types, and it will not affect the int type.

Problem solving code:

ElementType Average( ElementType S[], int N ){
    ElementType ans = 0;
    for(int i = 0; i < N; i++)
        ans += S[i];
    return ans*1.0/N;
}

Leave a Comment

Your email address will not be published. Required fields are marked *