CS240 Lab 2

Interest Calculation

Lab Exercise

Write a program that outputs the balance of a savings account at the end of every month.

The following is a sample output of your program (bold characters are what you type):


lore 46 % make
gcc lab02.c -o lab02.out
lore 47 % ./lab02.out
Amount of money to deposit every month: 200
Annual interest rate (%): 2

Month      Balance
==================
12         2426.16
11         2222.12
10         2018.43
 9         1815.07
 8         1612.05
 7         1409.36
 6         1207.02
 5         1005.01
 4          803.34
 3          602.00
 2          401.00
 1          200.33
lore 48 %
	

Tips and Hints

Reading Data from Keyboard

You are required to read numbers from the keyboard. To do this, use the getchar() function to read a character from the keyboard, then convert the character to an integer value. The getchar() function returns the ASCII code of the character pressed on the keyboard. Use the following to convert the ASCII value to the equivalent integer value:

int n = (int)getchar() - '0';

The statement above converts the output of getchar() to an integer by shifting it's ASCII value by 48 (The ASCII code of '0' is 48, check out the ASCII table). Therefore you will get a zero for the character '0', a one for the character '1' and so forth. After converting to an integer, it is a good practice to check if the input is valid. To do this, you check the value of n to ensure it is between 0 and 9. If you are not sure how this works, feel free to approach your TA and they will be more than happy to help.

Turnin

When you are satisfied that your program works correctly (or you run out of time), please do the following.

  1. Create a directory named lab02
  2. Copy all program files (*.c and *.h) and your Makefile to the directory created in step 1.
  3. Goto the parent directory of lab02 and type the following command into the terminal:

    turnin -c cs240=XXXX -p lab02 lab02

    where XXXX represents your lab section number.

    The turnin section is as follows:

    Section Time TA
    0201 Thursday 15:30-17:20 Dan Zhang
    0301 Friday 09:30-11:20 Suli Xi
    0401 Friday 13:30-15:20 Youhan Fang
    0501 Thursday 09:30-11:20 J. C. Chin

    Make sure turnin reports that your project was submitted for grading. You can check the files you have submitted by running the following command:

    turnin -c cs240=XXXX -v -p lab02
  4. Submit often to make sure that you did not try to submit after turnin has been turned off.

Grading Breakdown

2 points A working Makefile is provided.
4 points Program gets user input correctly.
5 points Finding balance correctly.
4 points Printing balance correctly.
5 points Printing balance starting from 12th month.