Hints on questions on reference parameters

This is the fourth programming milestone

  1. Modify the function you wrote in exercises on statics so that it returns both the mean and the number of observations, via suitable reference parameters. Test this carefully.
    The function could now either have three parameters, one the new value still pased by value, the others passed by reference (with an & in front), or it could have two parameters and use its return value to return one of the two it generates, while it uses a reference parameter to return the other. If you use three parameters, you can make the function into a void function.

    Whatever you do, you must still have the new value being passed by value, not by reference.

  2. Now rewrite the function to use a struct, passed in by reference, to return the sum, mean and number of observations. This allows the same function to keep records for multiple streams of numbers. Again test it carefully, for two or more separate streams.
    This is fairly straightforward. You will need to use parentheses to allow you to access members of the struct. Thus:
    (*st1).field1
    allows you to access element field1 in the struct st1 passed by reference. You will see more on this in the next note and in the note on accessing via pointers to structs.

Back to the questions.


Back to notes on reference parameters.