Please send questions to st10@humboldt.edu .

CIS 130 - Week 13, Monday, April 19, 2010

*   clicker question, recognizing a properly-structured
    sentinel-controlled loop; walked through working
    versions of the 4 answers;
    see posted correct answer, answer3.cpp

*   adding to spicy_sentinel.cpp:
    *   ...adding finding the maximum Scoville rating entered,
        and keeping track of how many have been seen:
        spicy_sentinel2.cpp

    *   ...adding keeping track of how many toddler-level
        ratings were given:
        spicy_sentinel3.cpp

*   what if there really isn't an appropriate
    sentinel value, but you want the user to indicate
    when to stop?

    ...you can just ask!
    "question"-based loop

    ask if they want to go on
    while( answer is yes )
    {
        get the data
        handle the data

        ask if they want to go on
    }

    *   something of a hybrid between count-controlled
        and a sentinel-controlled loop --

	you ask if they want to go on before and at the end
   	   of the loop (like sentinel-controlled),
	but you get and handle the data (often) at the
	   beginning of the loop (like count-controlled,
	   but without a count controlling it...)

    *   benefit? it can work for any kind of data;
        drawback? if there's very much data, the user may
	          get annoyed at having to answer yes so
		  much!!

    *   example: see spicy_ask.cpp

*   how about a loop that is none of these three?

    (possibly event-controlled...)

    what if you wanted a function that flakily-randomly
    chooses a number between 1 and 100, and it asks the
    user to guess what it is, until he/she does;
    then it returns the number of guesses it took?

*   how can you generate a flakily-random number in C++?

    in the standard C++ libraries:
    srand - expects a "seed" that seeds the pseudo-random
            number generator; people often use time(NULL)
    rand - expects nothing, and produces a pseudo-random
           integer between 0 and some implementation-dependent
	   max (often 32767 or something like that...)

    gee, we could use the modulo operator, % in C++,
    to make sure this random integer is in a smaller range!

    for example,
    (rand() % 100) would be guaranteed to be between 0 and 99 
    ...how can I get that to be between 1 and 100?
    	   1 + ((rand() % 100)

*   contract: guess: void -> int

*   purpose: expects nothing, flakily-randomly
    chooses a number between 1 and 100, and asks the
    user to guess what it is, until he/she does;
    then it returns the number of guesses it took

int guess()

*   examples:
    for  guess()
    ...if it takes me 3 guesses to guess the number,
    then  guess() == 3

// pseudocode

int num_guesses = 0;
srand( time(NULL) );
int target = 1 + ((rand() % 100)

ask the user for a guess
read it in
num_guess++;

while (target != guess)
{
    if (guess < target)
    {
        tell user to pick a higher number
    }
    else
    {
        tell user to pick a lower number
    }

    ask user to pick another number
    read it in
    num_guesses++;
}
    
return num_guesses

*   see final example: guess.cpp