The average of three byte values
No. 217
Q: |
A newbie programmer codes the following snippet:
A senior programming companion warns about two possible problems:
Explain both points in the answer box below. In addition provide a solution by modifying the above implementation without changing the method's signature. You may present your code in the answer box below as well. Alternatively correct the implementation of
de.hdm_stuttgart.mi.sd1.task4_no_unit_test. Tip
|
||||||||||||
A: |
Solving these flaws requires using a data type behaving free
of overflow errors with respect to the given context of summing up
three byte values. Choosing Dividing by 3 should be a floating point rather than an
integer operation to avoid cutting off fractional values. On top
we finally use the Finally we need a cast for converting back the rounded
value's type
Given our sum being within range [-3 * -128, 3 * 127]
dividing by three guarantees |