Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that type Money, a structured type with two int fields, dollars and cents

ID: 3661033 • Letter: A

Question

Assume that type Money, a structured type with two int fields, dollars and cents, has been declared. Also assume the availability of a function named normalize that receives a Money argument and returns a "normalized" Money value (i.e. one that is equivalent to the argument but where the cents values is between 0 and 99). Now write the definition of a function named addMoney that receives two Money arguments and returns their sum, in normalized form. So if the equivalent of $1.85 and $2.19 is passed the function would return the equivalent of $4.04.

Explanation / Answer

sir hope this helps


Pseudocode:

function addMoney( Money money1, Money money2 )
sum -> A Money object with dollars and cents each set to 0
set sum's dollars to ( money1's dollars + money2's dollars )
set sum's cents to ( money1's cents + money2's cents )
return normalize( sum )

function normalize( Money money )
cents -> money's cents
while ( cents - 100 ) is greater than or equal to zero...
set cents to ( cents - 100 )
increment money's dollars by one
set money's cents to the current value of variable cents
return money