Homework 10: Due Thursday, October 19th

Read Chapter 5, sections 5.5, 5.6, 5.7, and 5.9. Complete the following problems:

1. What will the following program segment print? Assume that all
variables are declared as int.

   total = 15;
   i = 0;
   k = 1;
   while (i < 3)
   {
      for (j = 6 ; j < 9 ; j++)
      {
         k = k + 1;
         cout << total << i << j << k << endl;
      }
      cout << total << i << j << k << endl;
      i++;
   }
   cout << total << i << j << k << endl;

2. Write a for loop that is equivalent to the following while loop:

      i = 10;
      while (i <= 1000)
      {
         . . . . .
         i += 5;
      }

3.   Write a while loop that is equivalent to the following for loop:

      for (j = 0 ; j < 27 ; j++)
      {
         . . . . .
      }

Show the output of the following program segments. Assume that all
variables have been defined as int.

4.  for (i = 2 ; i < 6 ; i++)         5.  for (i = 2 ; i < 6 ; i++)
       for (j = 15 ; j < 17 ; j++)        {
          cout << i << j << endl;            for (j = 15 ; j < 17 ; j++)
    cout << "done" << endl;                     cout << i << j << endl;
                                             cout << "outer" << endl;
                                          }

6.  j = 1;
    while (j <= 3)
    {
       sum = 0;
       for (k = 1 ; k < 5 ; k = k + 1)
       {
          cout << j << k << endl;
          sum = sum + j + k;
       }
       cout << sum << endl;
       j++;
    }

Write a C++ program segment using nested loops to create each of the
following outputs:

7.  1    1      8.  1    2    3    4    5    6    7    8    9
    1    2          1    2    3    4    5    6    7    8    9
    2    1          1    2    3    4    5    6    7    8    9
    2    2          1    2    3    4    5    6    7    8    9
    3    1          1    2    3    4    5    6    7    8    9
    3    2          1    2    3    4    5    6    7    8    9
    4    1          1    2    3    4    5    6    7    8    9
    4    2
    5    1
    5    2

9. A child's parents give the child $10 on her 12th birthday. They
promise to double the amount every year until the gift is more than
$1000. Write a the C++ statements to print out a table of the age of
the child and the amount of the gift. The table should start at age 12
and end when the gift is more than $1000. For example, the beginning
of the table might look like:

     Age     Gift
      12       10
      13       20
      14       40

Remember to type your answers and to make two copies of each homework assignment. One copy is submitted to me and the other you keep to use in the group discussions.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2004