csci 20 :: book learning challenge 6 (due tues, mar 1)
Submitting your work: Complete this exercise on your own.

Fill in all of the "Answer" boxes, then print the answer grid that you see after you press "Prepare for printing".

Staple all pages together and submit to the instructor on the date listed above.

Questions:

`Read Operators, Operators, C++ Loops, Statements and flow control, Conditionals and answer the following:

Your name:
  1. What are the three (3) types of loop statements in C++?
    Answer:
  2. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int countdown = 5;
    while (countdown >= 0)
    {
         cout << countdown << "..";
         countdown -= 1;
    }
    Answer:
  3. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    bool waiting = true;
    int elevatorOneFloor = 6;
    int elevatorTwoFloor = 1;
    while (waiting == true)
    {
         if (--elevatorOneFloor == 3)
         {
              cout << "Elevator ONE arrived." << endl;
              waiting = false;
         }
         else if (elevatorTwoFloor++ == 3)
         {
              cout << "Elevator TWO arrived." << endl;
              waiting = false;
         }
         else
         {
              cout << "Waiting." << endl;
         }
    }
    Answer:
  4. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int times = 5;
    while (times < 5)
    {
         cout << '#';
         times = times + 1;
    }
    Answer:
  5. What is wrong with the following while loop?
    int seconds = 0;
    while (seconds >= 0)
    {
         cout << seconds << endl;
         seconds++;
    }
    Answer:
  6. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int times = 5;
    do
    {
         cout << '#';
         times = times + 1;
    } while (times < 5);
    Answer:
  7. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int apples = 0;
    int oranges = 5;
    do
    {
         apples++;
         if (apples % 2 == 0)
              oranges++;
         cout << apples << " apples and " << oranges << " oranges" << endl;
    } while (apples < oranges);
    Answer:
  8. What is wrong with the following do-while loop?
    int seconds = 0;
    do
    {
         cout << seconds << endl;
         seconds++;
    } while (seconds >= 0);
    Answer:
  9. According to your text, what is the important difference between while loops and do-while loops in C++? What is the minimum number of times a while loop may execute? What is the minimum number of times as do-while loop may execute?
    Answer:
  10. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    for (int i = 0; i < 3; i++)
    {
         cout << "hello,";
    }
    Answer:
  11. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    for (int i = 5; i >= 0; i--)
    {
         if (i % 2 == 0)
              cout << "yes" << endl;
         else
              cout << "no" << endl;
    }
    Answer:
  12. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    string options [ ] = {"Soda", "Coffee", "Tea", "Water"};
    for (int i = 0; i < 4; i++)
    {
         cout << i << ". " << options[i] << endl;
    }
    Answer:
  13. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    string options [ ] = {"Soda", "Coffee", "Tea", "Water"};
    for (int i = 0; i < 4; i++)
    {
         cout << (i+1) << ". " << options[i] << endl;
    }
    Answer:
  14. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int max = 10;
    for (int i = 0; i > max; i--)
    {
         cout << "iterating..." << endl;
    }
    Answer:
  15. What would be the output of the following C++ code? If there is no output, write NO OUTPUT as your answer.
    int iterations = 0;
    int maxIterations = 50;
    int magicNumber = 4;
    while (iterations < maxIterations)
    {
         iterations++;
         cout << "iteration number " << iterations << endl;
         if (iterations == magicNumber)
              break;
    }
    Answer:
  16. Write a C++ while loop that will display the values 1 through 1000 in ascending order (each number on a new line) to the console. Use only the variable declared below in your loop.
    int nextNumber = 1;

    Answer:
  17. Write a C++ for loop that will display the values 1 through 1000 in ascending order (each number on a new line) to the console. Use only the variable (int i) that you declare in your for loop.
    Answer:
  18. Write a C++ do-while loop that (1) prompts the user for and reads a value between 1 and 100, (2) continues looping until the value is an even value (NOTE: question 11 above has a technique for testing to see if a number is even), and (3) breaks (using a break statement) if the input value is 7. Use only the variable declared below in your loop. Use inputValue = reader.readInt(1, 100) to read in the value.
    int inputValue = 0;

    Answer:
  19. Write a C++ for loop that will display the values 99 to 0 in descending order (each number on a new line) to the console. Use only the variable (int i) that you declare in your for loop.
    Answer:
  20. (CHALLENGING) Write a nested for loop (a for loop inside of a for loop, that displays the values 1 to 10 in ascending order, five (5) times. Do not hesitate to search for tips online using Google. If written correctly your output will look like the following:
    1 2 3 4 5 6 7 8 9 10
    1 2 3 4 5 6 7 8 9 10
    1 2 3 4 5 6 7 8 9 10
    1 2 3 4 5 6 7 8 9 10
    1 2 3 4 5 6 7 8 9 10

    Answer:

Total points for this book learning challenge:

150 points