2.12 An Extended Example (Bank Interest)

Example problem:

Compute the amount there will be in my bank account after one year if the interest is compounded quarterly and any withdrawals I make are entered immediately after the interest is paid.

Example problem suitability:

The bank account example given above is well within the reach of a simple calculator, and is therefore of somewhat marginal suitability for computer solution. However, the solution does illustrate how a number of the programming ideas discussed in chapter one are actually implemented in Modula-2.

Example problem restatement:

Given: A bank account, possibly containing money.

To Do: Compute interest, make withdrawals every 3 months

Desired Result: final balance.

Formula: interest = principal * rate * time.

Example problem library use:

This problem has both input and output requirements. It will, therefore make use of the ISO standard Modula-2 library. Because of the withdrawals, four separate simple interest calculations will be made, rather than one compound interest calculation. No mathematical or financial libraries are required.

Example problem refinement:

1. Input Section  obtain bank balance
			 obtain interest rate
			 obtain quarterly withdrawal amounts
2. Computation	 calculate interest each quarter
			 subtract withdrawal each quarter
3. Output		 print out final balance

Second refinement of problem: (same numbers as above)

1. Ask the user for the initial balance
	  Read the balance and assign the value to a real variable
	  Ask the user for the interest rate
	  Read the rate and assign the value to another real variable

2. set quarter to one
    while quarter is less than or equal to four
	For the current quarter
	 Compute the interest using
		interest = principal * rate * time
	 Add this to original balance
		balance = balance + interest
	 Ask user for withdrawal amount
	 Read the amount and assign the value to a real variable
	 Subtract the withdrawal from last balance
		balance = balance - withdrawal
	 add one to the quarter
    end while

3. After four quarters
	 Print out the final balance

Pseudocode:

In this refinement, only portions of the code that involve several steps, formulas, or computations (i.e. an algorithm) are detailed. In this example, the last natural language refinement is followed very closely, so this section might have been left out in this case, but it is included to make the model for students to follow as complete as possible.

quarter = 1
while quarter <= 4
	interest = principal * rate * time
	balance = balance + interest
  write message "what is withdrawal amount?"
  read (amount)
	balance = balance - withdrawal
  quarter = quarter + 1
end while

Example problem data table:

 Variables:	principal, rate, interest, withdrawal--all reals
			quarter--a cardinal
 Constants: time = 0.25
 Imports	from STextIO (InOut): WriteString, WriteLn, SkipLine
			from SWholeIO (InOut): WriteCard
			from SRealIO (RealInOut) ReadReal, WriteReal

NOTE: The purpose of SkipLine is to clear the end of line state that exists following one input line before attempting to read the next input line. This is necessary whenever inputs are separated by typing a carriage return. This was not previously needed, because only one input per program was being employed.

Example problem sample I/O:

Input:
What is the opening balance? 1000.00
What is the annual interest rate?
Enter as a decimal; Example: 6.5% is 0.065. 0.08
How much do you wish to withdraw in quarter number 1? 10.00
How much do you wish to withdraw in quarter number 2? 9.00
How much do you wish to withdraw in quarter number 3? 11.00
How much do you wish to withdraw in quarter number 4? 6.00
Output:
The final balance in your account is 1044.57

Example problem sample User Manual:

Description: Bank Interest is a small application designed to maintain a simple bank account record. It allows the user to specify the opening balance, and annual interest rate at the beginning of the program, and then to make quarterly withdrawals. Interest is calculated on the balance before the withdrawal.

Operation: If in an MS-DOS or UNIX environment, type the name of the program from the operating system prompt. If in a Graphics User Interface (GUI) environment, click twice in close succession on the icon representing the program.

A banner will appear giving the name of the program and some information about the author. Following this the prompt

What is the opening balance?

will appear on the screen. Type in the amount that the bank account had at the start of the year. Following this, the prompt

What is the annual interest rate?
Enter as a decimal; Example: 6.5% is 0.065.

will appear on the screen. Type in the interest rate in the form shown. Now, for each quarter, a message like

How much do you wish to withdraw in quarter number 1?

will appear on the screen. Respond with the amount being withdrawn for that quarter in the form 10.23. That is, give the number in decimal form and without a dollar sign. The program will finally respond with a message like:

The final balance in your account is xxx.xx

Example problem code:

MODULE BankInterest;

(*
   Name: Nellie Hacker
   Student Number: 922001
   CMPT 141 Fall 1991
   Assignment #1
   Bank Interest Computation
*)

FROM STextIO IMPORT
  WriteString, WriteLn, SkipLine;

FROM SWholeIO IMPORT
  WriteCard;

FROM SRealIO IMPORT
    ReadReal, WriteFixed;

CONST
  time = 0.25; (* that is, one quarter *)

VAR
  principal, rate, interest, withdrawal : REAL;
  quarter : CARDINAL;

BEGIN
  (* print informative material *)
  WriteString ("This program computes interest on an account");
  WriteLn;
  WriteString ("quarterly, and then allows for withdrawals.");
  WriteLn;
  WriteLn;
  WriteString ("It was written by");
  WriteLn;
  WriteString ("Nellie Hacker");
  WriteLn;
  WriteString ("for CMPT 141");
  WriteLn;
  WriteString ("Assignment #1 Due 1992 09 15");
  WriteLn;
  WriteLn;
  (* get user input *)
  WriteString ("What is the opening balance? ");
  ReadReal (principal);
  SkipLine;
  WriteLn;
  WriteString ("What is the annual interest rate? ");
  WriteLn;
  WriteString ("Enter as a decimal; E.g.: 6.5% is 0.065. ==>");
  ReadReal (rate);
  SkipLine;
  WriteLn;

  quarter := 1;
  WHILE quarter <= 4
    DO
      interest := principal * rate * time;
    (* compute new principal *)
      principal := principal + interest;
      WriteString ("What is the withdrawal in quarter number ");
      WriteCard (quarter, 0);
      WriteString ("? ==> ");  (* complete a readable prompt *)
      ReadReal (withdrawal);
      SkipLine;
      WriteLn;
    (* recompute principal *)
      principal := principal - withdrawal;
      quarter := quarter + 1;
    END;
(* print output *)
  WriteString ("The final balance in your account is $ ");
  WriteFixed (principal, 2, 0);
  WriteLn;
END BankInterest.

Example problem actual output (user input in bold):

This program computes interest on an account
quarterly, and then allows for withdrawals.

It was written by
Nellie Hacker
for CMPT 141
Assignment #1 Due 1992 09 15

What is the opening balance? 1000.00
What is the annual interest rate?
Enter as a decimal; E.g.: 6.5% is 0.065. ==> 0.08
What is the withdrawal in quarter number  1? ==> 11.00
What is the withdrawal in quarter number  2? ==> 9.00
What is the withdrawal in quarter number  3? ==> 5.00
What is the withdrawal in quarter number  4? ==> 6.00
The final balance in your account is $  1050.30

NOTE: When submitting such an assignment, students should include several sample runs with different data.

As indicated in the planning documents, the purpose of SkipLine is to read past the carriage return typed at the end of one piece of data so that the program can correctly access the next one. Technically, being at the end of a line is a "state" the input achieves. SkipLine removes any more input on the current line until this state is reached; then it passes the state by and readies the next "line" of data for reading.

2.12.1 Input in non ISO Versions

Non ISO standard libraries may employ a procedure ReadLine for essentially the same purpose as SkipLine. On the other hand, they may simply regard the end-of-line as a character to be read, rather than as a state to be in, and therefore must handle this differently. There are two possibilities:

1. Readxx statements may be set up to skip any "white space" (including carriage returns) before doing their read. If this is the case, the carriage returns do not need to be read separately, for they will be taken out of the input and thrown away by the next read statement.

2. They may require the user program to handle the carriage return as a character, but not supply any special procedure for doing so. In this event, the user program should import the procedure ReadChar or its equivalent Read from InOut, declare a character variable, say

  VAR
    cr : CHAR;

and replace all occurrences of SkipLine in the above program with

  Read (cr);

to read off and discard the carriage return.

In any event, the matter cannot simply be ignored, or the program will not operate correctly.


Contents