Lab 2: Control Flow: Conditional and Iterative Control Structures

The objectives for this assignment are:
  • Extend your Unix/Linux skills.
  • Gain experience in solving conditional and iterative problems, including nested control structures.
  • Learn to accurately format output on the console.
  • zyBook's zyLab

    The zyLab exercises for this week are visible and available on your zyBooks account.

    Getting Started

    As in the previous lab, make sure you create a lab2 directory in your dropbox. We recommend you do the same in your personal home directory. You will also find it easier to submit your files if you create a symbolic link.

    In-Lab Exercises

    Exercise 1: Write a program that finds and displays the roots to a single quadratic equation of the form ax2 + bx + c = 0
    The program is started here. Write the rest of the program, and demo it to a TA in your lab section.
    The program asks the user for the equation’s real coefficients a, b, and c ; it should then compute the discriminant, and determine how many solutions there are, and what they are.
    Assume all values are real (i.e. non-complex), and that coefficient a is non-null (so no need to check for it).

    Exercise 2: Write a program that computes, and displays, two sums:
      - the sum of the squares of numbers 1 through 10
      - the sum of the square roots of numbers 1 through 10
    You must only use a single loop structure for the two sums. Demo it to a TA.

    Part 1: Multiplication Table

    Write a program called table.cpp that displays an integer multiplication table. Ask the user for the size of the table on the X and Y axes (2 integers), then display a multiplication table exactly that big, with a top row header and a left column header. 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
    No need to check for 'crazy inputs' here (so you may assume that the user will always enter two reasonable integers). Just concentrate on the nested loops and the formatting. Use the iomanip library to make sure the numbers line up.

    Part 2: Mortgage Calculator

    In the housing crisis a few years ago, 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 (annual) interest rate times the remaining balance, increasing the balance due (side note: unfortunately, the interest gets charged before your payment is applied).

    Some loans are for a fixed amount of time, and the monthly payment is then calculated as a function of the initial loan amount and the interest rate. Other loans are determined by a desired monthly payment (if the borrower wants to control his/her cash flow), and the total time of the loan is then computed based on the initial loan amount and the desired monthly payment. In this problem, we will assume the latter.

    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! In the second month, the interest charge is $416.32, and the remaining balance is $99,832.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 (as you can deduce, most of the interest is paid early on).

    Month     Payment    Interest      Balance
    1         $500.00     $416.67   $ 99916.67
    2         $500.00     $416.32   $ 99832.99
    430       $500.00     $  3.97   $   457.01
    431       $458.84     $  1.90   $     0.00

    Your job is to write a program called mortgage.cpp that asks the user to input 3 values: the principal, the interest rate, and the desired monthly payment; and the program then displays an amortization table. At the end, the program should also display how long it took to pay off the mortgage (in years and months) as well as the total amount of payments over that time. For example:

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

    (Note: The values in the example above 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, add 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 enter 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.
  • 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 monthly payment is too small, balance will go up every month (since the interest will be higher than the payment)! 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.
  • Use the modulus operator to separate out the years and months.
  • Note that if you accidentally create an infinite loop, hit Control-C to stop the program.
  • Use the iomanip library to format the numbers.

    Part 3: ASCII Art Graphs

    Write a program (graph.cpp) 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 both of them at the end (if there are several maxima with the same value, display just one (anyone) of them; same for minima).

    Pick a non-trivial function with possible 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))
    Use any character of your choosing for display. A "*" or a "#" is a common choice.

    Select a range that shows some interesting behavior. Use iomanip to limit the displayed precision and line up the columns nicely.


    In C++, formatting is usually done using the iomanip class library. Make sure you add it in your program's header (after iostream):
          #include <iomanip>
    You can see some of its functions and some examples in section 7.3 of your zybooks.

    Lab Report

    Turn in table.cpp, mortgage.cpp, graph.cpp, and report.txt. Your lab report should explain the final part of the assignment (Part 3), explaining 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 Sunday, Sept 15th at 11 pm.

    All files must be submitted to your lab2 directory under the course dropbox (make sure you've created it!).
    Make sure you follow the guidelines from the general instructions for turning in.