Loop Worksheet

Show the output produced by the following segments of code. Assume that
all variables are declared as int.


1.  i = 30;                    2.  i = 10;
    j = 10;                        j = 10;
    if (i > 10 + j)                if (i > 10 + j)
       while (i > j)                  while (i > j)
       {                              {
          i = i - 10;                    i = i - 10;
          cout << i << endl;             cout << i << endl;
       }                              }
    else                           else
       while (j >= i)                 while (j >= i)
       {                              {
          j = j - 10;                    j = j - 10;
          cout << j << endl;             cout << j << endl;
       }                              }


3.  i = 30;                    4.  for (i = 1 ; i <= 10 ; i++)
    j = 10;                        {
    while (i >= j)                    if (i == 2)
    {                                    cout << "hello" << endl;
       if (i > j + 10)                else if (i == 3)
          cout << "yes\n";               cout << "goodbye" << endl;
       else                           else if (i == 4)
          cout << "no\n";                cout << "why" << endl;
       i = i - 10;                    if (i < 5)
    }                                    cout << "help" << endl;
                                      if (i > 6)
                                         cout << "blue" << endl;
                                   }



5.  w = 0;                            6.   w = 0;
    for (h = -2; h <= 5; ++h)              for (h = 10; h > 0; --h)
       w = w + h;                              cout << w;
       cout << w;

7.  n = 1;                            8.   for (x = 1; x <= 5; x++)
    for (k = 2; k <= 5; k++)                   cout << x << endl;
    {                                      cout << x << endl;
       n = k - 2 * 3;
       cout << k << ' ' << n << endl;
    }

9.  Given the following program segment:

          sum = finished = count = 0;
          while (count <= 8 && !finished)
          {
             cin >> number;
             if (number > 0)
                sum += number;
             else if (number == 0)
                finished = 1;
             count++;
          }
          cout << sum << ' ' << count << endl;

     What would be the output given this data?
        9 -3 4 6 0 5 6 0 5

10.  What does the following program segment do?

     punctuation = letters = digits = 0;
     cout << "enter any character or '/' to quit:  ";
     cin >> character;
     while (character != '/')
     {
         if (character == ',' || character == '.')
            punctuation++;
         else if (character >= 'a' && character <= 'z' ||
                  character >= 'A' && character <= 'Z')
            letters++;
         else if (character >= '0' && character <= '9')
            digits++;
         cout << "enter any character or '/' to quit:  ";
         cin >> character;
     }
     cout << punctuation << ' ' << letters << ' ' << digits << endl;

11.  Input a character and a number from the user.  Print "number" number of
     lines of output with the character printed once on each line.

12.  Write a program segment (including a while loop) that reads one number
into a varible named n and reads another number into a variable called
maxPower. Then your statements should raise n to each power 0, 1,
2, ..., maxPower, and print a table like the following (suppose n is 2
and maxPower is 4):

                      n raised
       n     power    to power
      --------------------------
       2       0          1
       2       1          2
       2       2          4
       2       3          8
       2       4         16

13.  Redo problem 8 using a for loop.

14. Given the following code, change the loop to an event controlled
loop that will exit when the user enters a negative number of hours
as the sentinel:

     cout << "Enter the number of employees: ";
     cin >> numEmployees;
     totpay = 0;
     empCount = 0;
     while (empCount < numEmployees)
     {
        cout << "Hours: ";
        cin >> hours;
        cout << "Rate: $";
        cin >> rate;
        pay = hours * rate;
        cout << "Employee pay is : $ " << pay << endl;
        totpay += pay;
        empCount++;
     }
     cout << "Total payroll is $ " << totpay << endl;


15. Write the statements to read in a group of exam scores ranging in
value from 0 to 100. Your program should count and print the number
of outstanding scores (90 to 100), satisfactory scores (70 to 89), and
the number of unsatisfactory scores (0 to 69). Stop reading exam scores
when a negative value is entered.


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

© Copyright Emmi Schatz 2002