The Guessing Game

Algorithm Design

We already decided that the next thing your program needs to do is input, is that what you wrote? We need to think about what "input" means when the computer does it, but that one word is a good place-holder while we think about the rest of the program.

Guess what? You already have the whole program (in English) staring you in the face. Well, maybe not. Recall that I said four pages ago that I would start off telling the other person what the Guessing Game does. I said

Hi. We are going to play the Guessing Game.
You think of a number, then I will ask some yes/no questions,
then I will tell you what number you thought of.
Are you ready?
That is a good English description of the program. It is the program, in a loosey-goosey way. Now let's you and me tighten it up some, mostly that second line. It will still be English, but let's describe more carefully what the computer will be doing:

You think of a number,

We already have code for that part: the introduction invites the user (output) to think of a number, then the computer (or the person playing the Guessor, in real life) waits for the other person to let us know that they have thought of a number between 1 and 99 (input).

I will ask some yes/no questions,

That's pretty vague. You and I know that when somebody says they will do "some things," they mean that they will do one thing, then another thing, then maybe a third thing, and so on until somebody decides that they should stop. That's iteration or repetition, the second of the Five Concepts in the list below. But the computer doesn't know that unless we tell it. So I'm going to replace this one line in our loose program with these four:
iterate
ask a question
input answer
next iteration
You see, this is still English, and still pretty vague, but a lot more precise than what I started out with. In particular, I changed a vague word "some" with a more detailed use of one of the Five Concepts. Also I put it in a table-like form, so it's easier to see what gets repeated (iterated, looped, same idea). Remember, when we tell the computer to "repeat" or "iterate" we are telling the computer that we are giving it a list of things to do, and it should do them over and over, several times, until it's time to stop. We humans still think of doing them all in one straight line, and the computer will do them in one straight line, but writing those steps over and over gets tiresome -- impossible if the number of repetitions is not known in advance -- so we tell the computer this is an iteration.

There. You have the whole program.

Actually, we are still in the design phase. Exactly what question is the computer going to ask? What question(s) would you ask, if you were the one doing the guessing? Think about what you would ask, not just the first question, but also the second, third, and fourth. How many questions do you need to ask before you know what number they thought of? Think about that for a while, then turn the page.

  
Five Basic Concepts
Sequence
Iteration
Conditional
Variables
Input/Output

 

<<Previous | ToC | Next >>

[2021 July 12]