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.

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.

LANGUAGE: C++

CHALLENGE:

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.

SOLUTION:


Money addMoney (Money one, Money two){
    Money temp;
    temp.dollars=one.dollars + two.dollars + (one.cents + two.cents)/100;
    temp.cents= (one.cents + two.cents) % 100;

    return temp;
}

Add comment