BU CAS CS 113
Introduction to Computer Science II with Intensive C
Fall 1997


Assignment 1: Getting Started with C


Last Modified: Fri Sep 12 09:48:22 1997

Deadline

September 16, 1997 (moved from September 15, 1997)

What to Submit

You should submit three files. Be sure the files you submit have exactly the following names: hw1-factor.c, hw1-pi.c, hw1-bank.c .

These programs are to be electronically submitted by using the submit program on csa. The code you submit should conform with the program assignment guidelines.

Assignment

The first two programs should be quite simple and are based on exercises in the book. The third is also not very difficult, but does combine a few more ideas together. Be sure to do a good job on exercise 3, and to write your code in such a way that it is easy to read and modify. You will get a chance to do just that as part of assignment 2, so you if you do a good job now, you will be ahead for the next assignment.
  1. Roberts Chapter 1, Exercise 8.

    The only input is an integer (use long to allow for larger numbers). Your program must display the factorization.

    Files: hw1-factor.c.

  2. Roberts Chapter 1, Exercise 11.

    One change from the assignment listed. Your program should have the user input the number of rectangles to use (the book says use 100). What will your program do if the user enters 0 or a negative integer?

    Files: hw1-pi.c.

  3. Savings and Loan

    One good way to save a large sum of money is to deposit each month a fixed sum of money into an interest bearing account (perhaps after making an initial deposit to open the account). Especially if the interest rate is relatively high, the money invested can grow quite significantly over time. This exercise we have you write a program which demonstrates this. (If you do not know how to compute interest in such an account, I have included an example below.)

    Your program should repeatedly

    Run the program with some resonable inputs. (Try $50 per month at 5% interest for 10 or 30 years, for example.) What happens if you enter a negative initial deposit? a negative payment? Be sure to include comments about these situations in your program output. Your output should look something like the following:

    
    Instructions go here.
    
    Be sure to add comments about negative input values.
    
    
    Enter interest rate (negative value to quit):  10
    Enter initial amount:  $1000
    Enter payment amount:  $100
    Enter number of years:  7
    
    
    
    initial amount: $1000.00; interest rate: 10.0000%; monthly payment: $100.00
    
    yrs mos      balance
      0   0      1000.00 
      0   1      1108.33 
      0   2      1217.57 
      0   3      1327.72 
      0   4      1438.78 
      0   5      1550.77 
      0   6      1663.69 
      0   7      1777.56 
      0   8      1892.37 
      0   9      2008.14 
      0  10      2124.87 
      0  11      2242.58 
      1   0      2361.27 
      2   0      3865.08 
      3   0      5526.36 
      4   0      7361.60 
      5   0      9389.02 
      6   0     11628.73 
      7   0     14102.96 
    
    
    Enter interest rate (negative value to quit):  -1
    
    

    To get you started, here is a shell you may use. It includes some of the basic structure, but you need to fill in most of the comments and code. There is also an example executable for this assignment in ~cs113/rpruim/F97/bin/ex1-bank. If your environment is set up correctly (so that ~cs113/rpruim/F97/bin/ is in your path) you can run this program by typing ex1-bank.

    Files: hw1-bank.c

    Computing Interest (an example). Suppose your account bears 12% interest annually. That's 1% per month, which means 1/100 = 0.01 (percent means per 100). Now suppose you make an initial deposit of $1000 and monthly deposits of $100. Initially you have $1000. At the end of 1 month, interest and your payement get added. The interest is 0.01 multiplied by the old balance (before you add the payment, you don't get interest on the payment until it sits in the bank for one month). So you have

    1000 + interest + payment = 1000 + (0.01 * 1000) + 100
                              = 1000 + 10 + 100
                              = 1110
    
    dollars in the account after one month. Similarly after 2 months you have
    1110 + interest + payment = 1110 + 11.10 + 100
    			  = 1221.10
    
    dollars.

    In general, interest is oldBalance * (annualRate/1200), and the new balance is the sum of the old balance, the interest, and the payment. Note that in C you really only need one variable balance not oldBalance and newBalance. Also, although it is not necessary to have a separate variable for interest, and in fact it makes the program slightly less efficient, it is still a good idea, since it makes the program easier to read, to debug and to modify. For this reason, YOU ARE REQUIRED TO USE A SEPARATE VARIABLE FOR INTEREST in the assignment.

Makefile

To check that your assignment compiles correctly, use the
Makefile for this assignment:
  1. Copy this Makefile into the directory where your programs are.
  2. Name it Makefile.
  3. type: make.

You can also use this Makefile to compile individual programs by typing, for example, make hw1-bank. The obvious generalization works for the other files as well.

I have provided a little test to see that your programs run correctly on inputs of the general form which will be used for grading. To run the test, type make test. If you want to have all of the output put into a file instead of onto the screen, type

	make test >& test.out

Can you figure out by looking at the Makefile what make tidy and make scratch do? Try it and find out.


Academic Honesty and Collaboration

It is reasonable to discuss with others possible general approaches to problems. It is unreasonable to work together on a detailed solution, to copy a solution, or to give away a solution. If your common discussion can be detected by looking at the solutions, then there is too much collaboration. Such instances of academic dishonesty may result in a course grade of F or expulsion from Boston University.

Do not allow your work to be used by others:

Warning: If someone cheats by using your work, you will also be penalized.