Lab 2: Control Flow

Sep 9: Added a note on mortgage rounding and corrected the due date.

This assignment assumes that you have read up through Chapter 3. Please bring your book to lab, as you will need it as a reference.

The objectives for this assignment are:

  • Extend your Unix skills.
  • Gain experience in solving iterative problems using loops and conditionals.
  • Learn to accurately format output on the console.
  • Getting Started

    As in the previous lab, start by creating a directory just for this assignment, and changing to that directory:
    mkdir cse20211/lab2
    cd cse20211/lab2
    

    Part 1: Multiplication Table

    Write a program called table.c that displays an integer multiplication table. Ask the user for the size of the table on X and Y axes, then display a table exactly that big. For example, if the user asks for a table that is 5 by 3, you should display the following:
       *    1    2    3    4    5
       1    1    2    3    4    5
       2    2    4    6    8   10
       3    3    6    9   12   15
    
    Note that each entry in the table is right-justified and occupies four characters, so that the numbers all line up. Consult Chapter 9.8 to see how to make printf use a field width of 4 for decimals. Test your program out to a very large number on the Y axis.

    Part 2: Mortgage Calculator

    In the recent housing crisis, many unsuspecting homebuyers got themselves into complicated mortgages that they couldn't afford, and perhaps didn't understand. Most mortgage companies go out of their way to hide from the buyer what the actual cost of purchasing a house is, so you should be able to calculate it for yourself.

    A mortgage is quite simple. The bank loans you a certain amount of money (the principal) to purchase a house at a certain interest rate. Every month, you must make a payment to reduce the balance. In addition, the bank charges interest by computing one twelfth of the interest rate times the remaining balance, increasing the balance due. (Unfortunately, the interest gets charged before your payment is applied.

    For example, suppose that you borrow $100,000 to purchase a home at 5 percent yearly interest. You agree to pay $500 per month until the mortgage is paid off. In the first month, the interest increases the balance by $416.67, then your payment reduces it by $500, for a remaining balance of $99,916.67. The first payment only reduced the principal by $83.33! (This is going to take a while.)

    In the second month, the interest charge is $416.31, and the remaining balance is $99832.99. And so on. If you keep computing like this, you get what is known as an amortization table that shows every payment until the mortgage is paid off.

    Month     Payment    Interest      Balance
    1         $500.00     $416.67   $ 99916.66
    2         $500.00     $416.32   $ 99832.89
    ...
    430       $500.00     $  3.97   $   457.01
    431       $458.84     $  1.90   $     0.00
    

    Write a program called mortgage.c that asks the user to input the principal, interest rate, and desired monthly payment, and then displays an amortization table. At the end, the program should display how long it took to pay off the mortgage (in years and months) and the total amount of payments over that time. For example:

    You paid a total of $215458.92 over 35 years and 11 months.
    

    (Note: These examples were calculated by using a float to represent the balance, so you will notice some oddities in the rounding, as the program is keeping track of sub-cent values. Your bank has some specific rules for rounding off cents on each calculation, but we won't get into that level of detail.)

    Once you have the basic calculation working, solve the following refinements:

  • If the user gives an invalid numeric input (like a negative interest rate), your program should display an error message, then prompt to the input again. You do not have the worry about the user entering letters instead of numbers.
  • For dollar values, only display two digits of precision after the decimal point. Consult Chapter 9.8 in the Deitel book for examples.
  • The final payment will almost certainly be smaller than all the others, so be careful to check for that case so you don't end up with a negative balance.
  • If the desired payment is too small, balance will go up every month! If this happens, the program should stop and display an appropriate error message.
  • And here are some hints to help you:
  • Start with some simple examples that complete with a few payments, then verify your results by hand.
  • If you accidentally create an infinite loop, hit Control-C to stop the program.
  • Use the modulus operator to separate out the years and months.
  • Part 3: ASCII Art Graphs

    Write a program (graph.c) that generates a simple ASCII Art graph of a mathematical function. The program should state the function and range and then display the graph. To make it easier, the X axis will run vertically down the page. Each line should show the X and Y values, then a bar proportional in length to the Y value. Along the way, the program should observe when the maximum and minimum over the range occur, and display them at the end.

    Pick a non-trivial function with multiple maxima and minima that makes an interesting pattern. It could be a high-degree polynomial or some combination of transcendental functions. As a simple example, here is the output for 10.0*(1.0+sin(x)). Select a range that shows some interesting behavior. Again, use printf to limit the displayed precision and line up the columns nicely.

    Lab Report

    Please review the general instructions for lab reports.

    Turn in table.c, mortgage.c, graph.c, and report.txt. Your lab report should explain the final part of the assignment, explaning how it works from the user perspective, how the program works internally, and how you verified that the output of the program is correct.

    Turning In

    This assignment is due on Monday, Sept 16th at noon. Late assignments are not accepted.

    Please review the general instructions for turning in.