CS Logo

Homework 3
 

This homework is intended to gauge your understanding of iteration and how to write basic functions, it is due on Thursday the 26th.

 

1) (15 points) Write a program which gets two integers from the user and finds their sum using a while loop. You may use only the unary increment operator (++), not the binary + operator.

 

2) (15 points) Write a program that sums the odd integers between 1 an 99 (don't include 1 and 99) using a for loop.

 

3) (15 points) Write a program that prints a multiplication square for 1 through 10 using nested for loops.

 

Your output should look something like this:

   1   2   3   4  5  6  7  8  9  10 

   2   4   6   8  10  12  14  16  18  20 

   3   6   9  12  15  18  21  24  27  30 

   4   8  12  16  20  24  28  32  36  40 

   5  10  15  20  25  30  35  40  45  50 

   6  12  18  24  30  36  42  48  54  60 

   7  14  21  28  35  42  49  56  63  70 

   8  16  24  32  40  48  56  64  72  80 

   9  18  27  36  45  54  63  72  81  90 

  10  20  30  40  50  60  70  80  90 100

 

4) (25) Consider the following program which computes the growth rate of $10,000 at 4% interest compounded yearly:

 

   #include <iostream.h>


   int main()
   {
      // Program to compute compound interest
     int years = 3;

     float rate = 0.04, amount = 10000.0;
  
      years=0;
      amount=amount+amount*rate;
      years++;
      cout << "Amount after year " << years << " is " << amount << endl;
      amount=amount+amount*rate;
      years++;
      cout << "Amount after year " << years << " is " << amount << endl;
      amount=amount+amount*rate;
      years++;
      cout << "Amount after year " << years << " is " << amount << endl;
 

   return 0;

   }
 

Now, using a for loop, write a program that gets the values amount, rate, and years from the user and prints the value of amount after each year.

 

 5) (30) Write a function called return_on_investment which takes amount, rate and years as arguments and returns the value of amount after years. Don't print the value of amount for intermediary years.

 

Now write a program which gets amount, rate, and years from the user, passes those values to your function return_on_investment and prints "Your investment of $starting amount compounded at rate percent would be worth $final amount in years years." where the italicized parts are your variable (or function) values.