Next: Arrays Up: Further User-defined functions in Previous: Review Questions

Exercises

  1. Write a function
    void floattopp(float q, int& L, int& P)
    
    which converts the sum of money q in pounds into L pounds and P pence where the pence are correctly rounded. Thus if q was 24.5678 then L should be set to 24 and P should be set to 57. Remember that when assigning a real to an integer the real is truncated. Thus to round a real to the nearest integer add 0.5 before assigning to the integer.

    Write a simple driver program to test the function. Think carefully about the boundary conditions.

  2. It is required to print out a table of mortgage repayments for a range of years of repayment and a range of rates of interest. Write an algorithm to produce a table of the monthly repayments on a mortgage of 40,000 pounds repayable over 15, 16, 17,..., 30 years with interest rates of 3, 4, 5,..., 10 per cent. Assume that the actual repayment will be produced by a function which will take as input parameters the principle P, the repayment time n in years and the rate of interest r as a percentage rate and will return the monthly repayment in pounds and pence.

    Transform your algorithm into a C++ program. Write a function prototype for the repayment calculation function and write the function itself as a function stub. That is a function that does not perform the correct calculation but delivers values that are sufficient to test the rest of the program. In this case it should return values of zero for the pounds and pence of the monthly repayment. This will allow you to test that your program lays the table out properly.

    Once you have the table layout working you can now write the function itself. The repayment each month on a mortgage of P pounds, taken out over n years at an interest rate of r% is given by:

    \begin{displaymath}
repayment = \frac{r P k^{12n}}{1200(k^{12n}-1)}
\end{displaymath}

    where

    \begin{displaymath}
k = 1 +\frac{r}{1200}
\end{displaymath}

    In writing this function use the function power from Section 21.5. This means you must add this function, and a suitable prototype, to your program. Also use the function of exercise 1 above.



Next: Arrays Up: Further User-defined functions in Previous: Review Questions