CSCI 297 Practice 2

Time to Pay a Debt

The calculations in this assignment involve the length of time required to pay off a debt at a specific rate of interest, paying the same amount once a month.

To make life simple, we are going to assume that an individual named John Smith owes the sum of $20,000.00 to a single creditor. The annual rate of interest R on the debt is some percentage, expressed as a decimal such as 0.05 for 5% or 0.12 for 12%. Each month, Smith makes a payment of X dollars on this debt, and at the end of the month, Smith's balance B increases by the amount B * R / 12. (That is, the monthly rate of interest is 1/12 of the annual rate.)

We want to know how long (how many months and years) it will take for Smith to pay off this debt. We will assume the debt is never increased other than to add interest. The data is set up so the balance will eventually reach 0.


What does the data look like?

It consists of lines each of which contains an interest rate R (for example: 0.12) and the amount of the monthly payment X (for example: 250.00).

Copy the data file into your directory. You can find it here.


What should the program do?

Open the input file. You will need to assign it a UNIT number. (Do not use 5 or 6.)

Your program will read the data, one line at a time, and compute payoff time. It will then print a line containing the two input values and the payoff time (measured in years and months).

The initial amount of the balance B is $20,000. During each month, we have two transactions: subtract the payment X and then add the interest.

Your program will use a loop to compute the payoff time. Each iteration of the loop corresponds to one month. In this loop, your need to do the following:

          If (B < X) Then
            B = 0
            Add 1 to your month-counter
          Else
            B = B - X
            B = B + B * R / 12
            Add 1 to your month-counter
          End If

Your program should next convert the payoff time into years and months. For example, a payoff time of 29 months is equal to 2 years and 5 months.

We are referring here to amounts of money, and we do not want to deal with fractions of a penny. When we compute the interest for one month, we need to round off the result to the nearest penny. How can we do that? Here is one way:

Suppose I is the amount of interest, a REAL value such as 273.877 or 273.874. Declare an INTEGER variable J and set J = 100.0 * I. Now I * 100.0 - J is just the value 0.7 or 0.4 or something like that. If I * 100.0 - J >= 0.5, we want to round up, so we want I = (J + 1) / 100.0. Otherwise we want to round down, so we want I = J /100.0.

We are going to have slightly fancier output than we have had before. Print a page heading and column headings at the top of the output. Print only 15 lines of output per page (not counting the page and column headings), double-spaced. Include a page number in the page heading. There will be several pages of output. Skip 4 lines to indicate a new page.

Your program should be well structured and properly documented. You should have a box of comments at the top of the program providing your name and identifyting which assignment this is and giving a brief description of what it does. Try not to use any extra variables.

Your program should use IOSTAT to find the end of the input file. Read about this if you need to.

Close the input file when you are done with it.

You may also want to use a subroutine to print the page heading when it is needed. You will need to invent a name for it, something like "PrintHeading". It should have two parameters, the counter you use for lines per page and the counter you use for pages. You should have a block of comments at the top of your subroutine giving its name and describing its parameters and what it does.

As before, you need to compile, link and run program, correcting any errors. Name your program "prac2.f90". Run the program using output redirection, reading the data in "prac2dat.txt" and creating an output file called "prac2.out".