---
title: "Function template: sum and average of array"
subject: "Object Oriented Programming"
chapter: 9
key: "semester_3/object_oriented_programming:function-template-sum-average"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-template-sum-average
site: BCT Engineering
---

# Function template: sum and average of array

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2070 Asar, 6 marks
- Video: [Passing a Array to a Function Template in C++](https://www.youtube.com/watch?v=DzIWeryyd3Y) — Thom MacDonald
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-template-sum-average
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

Write a program that will find the sum and average of elements in an array using function templates.

## Short answer

Define function templates sum and average. sum accumulates elements, average returns (double)sum/n. For int array {10,20,30,40,50}, sum=150, average=30; for float array {1.5,2.5,3.5,4.5}, sum=12, average=3.

## Answer

```cpp
#include <iostream>
using namespace std;

template <class T>
T sum(T arr[], int n) {
    T total = 0;
    for (int i = 0; i < n; i++)
        total += arr[i];
    return total;
}

template <class T>
double average(T arr[], int n) {
    return (double)sum(arr, n) / n;
}

int main() {
    int a[5] = {10, 20, 30, 40, 50};
    float b[4] = {1.5f, 2.5f, 3.5f, 4.5f};

    cout << "Integer array: sum = " << sum(a, 5)
         << ", average = " << average(a, 5) << endl;
    cout << "Float array:   sum = " << sum(b, 4)
         << ", average = " << average(b, 4) << endl;
    return 0;
}
```

**Output**

```text
Integer array: sum = 150, average = 30
Float array:   sum = 12, average = 3
```

The type `T` is deduced from the array argument, so the same functions work for `int` and `float` arrays. The average is returned as `double` so that integer arrays do not lose the fractional part.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the compiler know T when sum(a, 5) is called with an int array?
   A. T must be written as sum<int>
   B. T is always double
   C. T is decided at run time
   D. It deduces T from the array argument
   Answer: D. T is deduced from the type of the argument array. (Key point: Type deduction)

2. Why does average() return double?
   A. So an integer array does not lose the fractional part
   B. Because sum returns double
   C. Because the array is double
   D. To save memory
   Answer: A. Returning double (with the sum cast) avoids integer division truncating the result. (Key point: Average return type)

3. For b = {1.5, 2.5, 3.5, 4.5}, what are the sum and average?
   A. 10 and 2.5
   B. 12 and 3
   C. 12 and 4
   D. 8 and 2
   Answer: B. Sum is 1.5+2.5+3.5+4.5 = 12 and average is 12/4 = 3. (Key point: Result of float array)

## Related in Object Oriented Programming

- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
- [Importance of function template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-importance) — Object Oriented Programming, chapter 9, asked 2×
- [Need of template; function template overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Awhy-template-function-overloading) — Object Oriented Programming, chapter 9
- [Function template power(base, exp)](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apower-function-template) — Object Oriented Programming, chapter 9

## Related in other subjects

- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Sum and average between M and N using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-sum-average-range) — Computer Programming, chapter 9
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
