Assignment 12
Due Monday, February 26 at 9:20am on Canvas


Assignment 12.1: What's Your Sign? (10 pts)
  • For this assignment, let's write an astrology program.
  • Welcome the user (see sample output below)
  • Prompt the user to enter a numerical value (1-12) for the month of his/her birth (see sample output)
  • Prompt the user to enter a numerical value (1-31) for the day of his/her birth (see sample output)
  • Then, determine the user's sign based on their input.
        Aries                March 21-April 19
        Taurus             April 20-May 20
        Gemini            May 21 - June 21
        Cancer            June 22 - July 22
        Leo                  July 23 - August 22
        Virgo                August 23 - September 22
        Libra                September 23 - October 22
        Scorpio            October 23 - November 21
        Sagittarius       November 22 - December 21
        Capricorn        December 22 - January 19
        Aquarius         January 20 - February 18
        Pisces            February 19 - March 20
  • Your program must include a while loop to allow the user to repeat the calculation for another date of birth.
  • Expect to need to use if, else if and else statements.
  • Expect to use logical operators (&&, ||).
  • Submit your source code to Canvas when finished

Your sample output must be identical to mine:




Assignment 12.2: Hickory Dickory Dock Again (10 pts)
  • Open up hickory.cpp from assignment 11.
  • Alter the program to use a for loop instead of a while loop
  • When you are finished, and your output looks like mine, upload your assignment to Canvas.
Your Output Should Look Like the Following. Note that some values have been omitted for brevity.
1
2
Hickory!
4
Dickory!
Hickory!
7
8
Hickory!
Dickory!
11
Hickory!
13
14
Dock! The Mouse Ran Up the Clock!
16
17
Hickory!
19
Dickory!
Hickory!
22
...
149
Dock! The Mouse Ran Up the Clock!


Assignment 12.3: x and y (10 pts)
  • Create a new C++ file called xy.cpp.
  • In this assignment, we are going to write another program that uses a for loop. 
  • Within the loop, make the variable x go from -5 to 5, counting by 0.5. (This means that x can't be an int.)
  • Inside the body of the loop, set another variable y to be the current value of x raised to the fifth power
  • For output, you will need to display the current values of both x and y in a chart, as shown below.
  • You will need to use tab, fixed and setprecision(n) and the pow(base,exp) function here. (What library(ies) do you need?)
  • When you are finished, upload your source code to Canvas.
Your Output Should Look Like the Following.

x       y
-5.0    -3125.0
-4.5    -1845.3
-4.0    -1024.0
-3.5    -525.2
-3.0    -243.0
-2.5    -97.7
-2.0    -32.0
-1.5    -7.6
-1.0    -1.0
-0.5    -0.0
0.0    0.0
0.5    0.0
1.0    1.0
1.5    7.6
2.0    32.0
2.5    97.7
3.0    243.0
3.5    525.2
4.0    1024.0
4.5    1845.3
5.0    3125.0


Read About and Run the Code for These Loops:

  • Let's look at 4 common loop applications:

1. Indefinite Loops

Recall our looping application that simulated the play of an exciting game


#include <iostream>
using namespace std;

int main() {
    string repeat = "y";
    while ("y" == repeat) {
        cout << "\nPlaying an exciting game!\n";
        cout << "Do you want to play again? (y/n) ";
        cin >> repeat;
    }
    cout << "\nThanks for playing!\n";

    return 0;
}
  • Loops of this type are called indefinite loops because you do not know in advance how many time the loop will execute
  • This behavior is different from a counting loop where you know how many times the loop will execute before the loop starts
  • With an indefinite loop we can solve a new set of problems
  • Most problems solved with indefinite loops make use of while statements


2. Processing a Sequence of Inputs
  • Another common use for indefinite loops is to process a sequence of inputs
  • As an example, let us add up (sum) a series of numbers
  • Every number is added to the sum
  • We use a loop to repeat the input until the user decides to stop
  • Since we do not know how many number the user will enter, we use an indefinite loop as shown below

#include <iostream>
using namespace std;

int main() {
    double input = 1;
    double sum = 0;
    string repeat = "y";

    cout << "I will add up numbers for you\n\n";
    while ("y" == repeat) {
        cout << "So far, sum = " << sum << endl;
        cout << "Enter a number: ";
        cin >> input;
        sum = sum + input;

        cout << "Another number? (y/n) ";
        cin >> repeat;
    }
    cout << "Ending sum: " << sum << endl;

    return 0;
}

3. Terminating the Input with a Sentinel

  • Whenever we read a sequence of input values, we need to have some way of terminating the input loop
  • We could use a separate variable and input statement as we have done before:
    string repeat = "y";
    while ("y" == repeat) {
        // ... statements to repeat
        cin >> repeat;
    }
    
  • However, when entering numbers (or other data) repeatedly, answering an extra question each time through the loop becomes annoying
  • One way to avoid asking an extra question is to use a sentinel value
  • sentinel is guard who watches for something to happen

        image source

  • Similarly, a sentinel in a program watches for a specific sentinel value that signals termination of a loop
  • To use a sentinel value, we must have a special value in the input
  • Some commonly used sentinel values for numeric input are 0 or -1
  • The following program is an update of the previous program to use a sentinel value to end the loop
    • Question: What is the sentinel value for this loop?

Example Application Using a Sentinel Value for the Loop Test

#include <iostream>
using namespace std;

int main() {
    double input = 1;
    double sum = 0;

    cout << "I will add up numbers for you\n\n";
    while (input != 0) {
        cout << "So far, sum = " << sum << endl;
        cout << "Enter a number or 0 to exit: ";
        cin >> input;
        sum = sum + input;
    }
    cout << "Ending sum: " << sum << endl;

    return 0;
}


4. Error Checking: Input Validation

  • Another common use for indefinite loops is input validation
  • Input validation combines a loop with one or more if statements
  • The input statement is placed inside the loop
  • The if-statement tests for an incorrect input value
  • The loop repeats while the user's input contains an error
  • Since we do not know how many times the loop must execute ahead of time, the loop is indefinite
  • For example, the following program uses a loop to ensure a user enters a positive number
  • The if statement is used to decide when to output an error message

Example of Input Validation Using a Loop


#include <iostream>
using namespace std;

int main() {
    double input = 0.0; // initialize value
    while (input <= 0) {
        cout << "Enter a positive number: ";
        cin >> input;
        if (input <= 0.0) {
            cout << "You must enter a positive number\n";
        }
    }
    cout << "You entered: " << input << endl;

    return 0;
}
  • We will explore all of these applications in our programs as we continue to write loops.