10.8k views
1 vote
Write the definition of a function named averager that receives a double parameter and returns-- as a double -- the average value that it has been passed so far. So, if you make these calls to average, averager(5.0), averager(15.0), averager(4,3), the values returned will be (respectively): 5.0, 10.0, 8.1.

1 Answer

4 votes

Answer:

double averager (double &newNumber)

{

static double sum = 0.0;

static int counter = 0;

counter++;

sum += newNumber;

return sum / counter;

}

Step-by-step explanation:

This exercise is for you to understand how to use variables in functions that have scope outside of the function. The static keyword before a initialising a variable whether int or double or float or string, place the variable on the STATIC MEMORY. This means that when the function ends, those static variables are still usable and can be manipulated with the same stored values. So when the function sees the

static double sum = 0.0;

for the second time it IGNORES it and continues with the previously stored value.

User Gene M
by
5.3k points