CS 107 - Lab 5

Loops

In lecture we have discussed the three basic loop statements. Note that the description given below are the Each loop will keep executing the statement as long as the condition is true. Any statement can be block statement to allow for multiple statements to be in the body of the loop.

The while loops and for loops are the most common loops used. All for and do-while loops can be written as while loops (the simpliest form of the looping statements), but rarely does a program contain only while loops.

Nested Loops

Often a loop will have another loop nested inside of it. These loops are often called the outer and inner loops. A simple example of this is to create a multiplication table as follows:
     // printout the column headings
     cout << "    :";
     for (i = 1; i <= 10; i++)
         {
          cout.width(4);
          cout << i;
         }
     cout << endl;

     // print out 10 rows with a row heading
     // outer loop
     for (i = 1; i <= 10; i++)
         {
          cout.width(4);
          cout << i << ":"

          // print out the products for the row and column values
          // inner loop
          for (j = 1; j <= 10; j++)
              {
               cout.width(4);
               cout << i*j;
              }
           cout << endl;
          }

Lab Assignment

Due: By your Lab Time during the sixth week of the semester (the week of 2/17/2003).

You may turn in the assignment to your TA during lab or place it in his mailbox in 905 SEO. It is suggested that you place it in his mailbox just in case you are unable to attend lab. You are to hand in a print out of your program to do the following to your TA.

Create a C++ program on your UIC icarus account that will calculate the number of months needed to payoff a loan and the amount of interest paid on the loan. Your program should prompt the user for:

Your program should first make sure that the monthly payment is large enough to pay off the monthly interest and some of the debt. If the (amount of the loan) * (monthly interest rate) >= monthly payment, then the monthly payment is NOT large enough to payback the loan.

Once you know the monthly payment is large enough, use a loop to calculate the amount of interest and the size of the debt after each month. (You may wish to print out these values, but you are not required to.) Note the last payment may be less and the normal monthly payment amount.

To understand how this is done, let us look at the following example. Assume you have a loan of $1000, with a monthly payment of $50 and monthly interest rate of 1.5%.

Hand in a print out of your program to your TA.