Loop Worksheet Answers


1.  20               2.  0
    10

3.  yes              4.  help
    no                   hello
    no                   help
                         goodbye
                         help
                         why
                         help
                         blue
                         blue
                         blue
                         blue

5.  12 (indentation is incorrect)

6. 0000000000

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

9.  19 5

10.  The program counts the number of times the characters "." and ","
are entered using the variable "punctuation", It counts the number of
times a letter is entered using the variable "letters", and it
counts the number of times a digit is entered using the variable
"digits".  All other characters are ignored. When the user enters
the sentinel (the character '\'), the program stops reading
characters and prints the three counts.

11.  cout << "please enter a character followed by a number  ";
     cin >> character >> number;
     for (lines = 1; lines <= number; lines++)
    cout << character << endl;

12.  cout << "Enter n: ";
    cin >> n;
    cout << "Enter the maximum power for n: ";
    cin >> maxPower;
    cout << "                       n raised\n";
    cout << "       n     power     to power\n";
    cout << "      --------------------------\n";
    nRaised = 1;
    pow = 0;
    while (pow < maxPower)
    {
       cout << setw(8) << n << setw(8) << pow << setw(11)
            << nRaised << endl;
       pow++;
       nRaised = nRaised * n;
    }

13.  cout << "Enter n: ";
    cin >> n;
    cout << "Enter the maximum power for n: ";
    cin >> maxPower;
    cout << "                       n raised\n";
    cout << "       n     power     to power\n";
    cout << "      --------------------------\n";
    nRaised = 1;
    pow = 0;
    for (pow = 0 ; pow < maxPower ; pow++)
    {
       cout << setw(8) << n << setw(8) << pow << setw(11)
            << nRaised << endl;
       nRaised = nRaised * n;
    }

14.  totpay = 0;
     cout << "Hours: ";
     cin >> hours;
     while (hours >= 0)
     {
        cout << "Rate: $ ";
        cin >> rate;
        pay = hours * rate;
        cout << "Employee pay is : $ " << pay << endl;
        totpay += pay;
        cout << "Hours: ";
        cin >> hours;
     }
     cout << "Total payroll is $ " << totpay << endl;



15. int numout, numsat, numunsat;
    int score;

    numout = numsat = numunsat = 0;
    cout << "Enter an exam score: ";
    cin >> score;
    while (score >= 0)
    {
       if (score >= 90)
          numout++;
       else if (score >= 70)
          numsat++;
       else
          numunsat++;
       cout << "Enter an exam score: ";
       cin >> score;
    }
    cout << "\nOutstanding scores: " << numout << endl;
    cout << "Satisfactory scores: " << numsat << endl;
    cout << "Unsatisfactory scores: " << numunsat << endl;


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

© Copyright Emmi Schatz 2002