# Writing Pseudocode And Making a Flow Chart A Number Guessing Game

## Presentation on theme: "Writing Pseudocode And Making a Flow Chart A Number Guessing Game"— Presentation transcript:

Writing Pseudocode And Making a Flow Chart A Number Guessing Game
3/28/2017 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II Copyright 2004, Richard T. Vannoy II

Learning Objectives To read a word problem and create:
3/28/2017 To read a word problem and create: Correct Pseudocode, and An accurate flow chart Copyright 2004, Richard T. Vannoy II

The Problem: The problem is an old number guessing game.
3/28/2017 The problem is an old number guessing game. The computer picks a number from 1 to 100, and you have 7 tries to guess the number it picked. Produce the pseudocode Create an accurate flow chart using Visio Copyright 2004, Richard T. Vannoy II

The Solution: 3/28/2017 One, organized way to approach this problem uses the acronym D.I.P.O. D = Declare I = Input P = Process O = Output Copyright 2004, Richard T. Vannoy II

D.I.P.O. D = Declare (the variables)
3/28/2017 D = Declare (the variables) The First Step What numbers will need to be stored? What names will need to be stored? What other data needs to be stored? Anything used or manipulated by the computer must first be stored in memory. Copyright 2004, Richard T. Vannoy II

D = Declare 3/28/2017 For the number guessing game we will need someplace to store… the number the computer picks. (We’ll call this variable number) the number of guesses made by the human. (We’ll call this variable howMany) The number the human enters as a guess. (And call this variable guess) Copyright 2004, Richard T. Vannoy II

I = Input The input for this program is easy…
3/28/2017 The input for this program is easy… The human’s guess is the only input Copyright 2004, Richard T. Vannoy II

P = Process The process for this program is easy…
3/28/2017 The process for this program is easy… Read the human’s guess from the console Compare the guess to the computer’s number Route to “Guess again” (with a hint) or “Game over” depending on human guess Copyright 2004, Richard T. Vannoy II

O = Output 3/28/2017 During the guessing process, there will be four possible outputs: “Your guess was too low!” “Your guess was too high!” “You Got IT!!!”, (You win!) and “Too many guesses. (You lose.)” …and at the end, the output: “Play Again?” Copyright 2004, Richard T. Vannoy II

The Chicken or the Egg? Which comes first?
3/28/2017 Which comes first? The pseudocode? or The flow chart? Actually, it doesn’t matter…. Do one or the other first, or Do them both, side-by-side, at the same time. Copyright 2004, Richard T. Vannoy II

The Pseudocode 3/28/2017 Because of this PowerPoint format, it would be cumbersome to do both together, so I’ll make the choice most programmers would pick: pseudocode first. At this point, it would be good to have all the previous slides printed out and available, so that you can better see all the requirements and components. Copyright 2004, Richard T. Vannoy II

The Pseudocode 3/28/2017 The “Big Picture”, meaning lacking details or specifics of the pseudocode would look like this. Computer picks a number Human gets 7 guesses Possible responses: - Too high - To low - Correct! You win! - Too many guesses. You lose. Play again? Copyright 2004, Richard T. Vannoy II

Modules/Subroutines 3/28/2017 Although this program is too small to normally warrant the use of several modules or subroutines, I will use them here just to help you keep in mind the idea that separate tasks should be in individual modules. Copyright 2004, Richard T. Vannoy II

Modules Here is what the “big picture” flow chart looks like in Visio:
3/28/2017 Here is what the “big picture” flow chart looks like in Visio: Copyright 2004, Richard T. Vannoy II

Modules 3/28/2017 Next, let’s try to write some pseudocode to match each of these shapes. (Look at the previous slides and apply what you know here.) Copyright 2004, Richard T. Vannoy II

Modules Initialize: (We need to store) integer number (1-100)
3/28/2017 Initialize: (We need to store) integer number (1-100) integer guess (1-100) integer howMany (1-7) Copyright 2004, Richard T. Vannoy II

Modules Process: (Do this up to 7 times) Get human guess
3/28/2017 Process: (Do this up to 7 times) Get human guess Compare guess with number Output message: Too high! (Repeat guess) Too low! (Repeat guess) You win! (Exit to Play Again) You lose. (Exit to Play Again) Copyright 2004, Richard T. Vannoy II

Modules Play Again: If YES, return to start If NO, end program
3/28/2017 Play Again: If YES, return to start If NO, end program Copyright 2004, Richard T. Vannoy II

Complete Pseudocode Initialize: (We need to store)
3/28/2017 Initialize: (We need to store) integer number (1-100) integer guess (1-100) integer howMany (1-7) Process: (Do 7 times) Get human guess; Compare with number Based on Guess, Output message: Too high! (Repeat guess) Too low! (Repeat guess) You win! (Exit) You lose (Exit) Play Again: If YES, return to start If NO, end program Copyright 2004, Richard T. Vannoy II

What’s Next? 3/28/2017 So… We have the big picture and the major items of both the pseudocode and the flow chart Now we need to go into more detail. This is called “Top Down” design Copyright 2004, Richard T. Vannoy II

Top Down? 3/28/2017 Top Down means to start with the big picture, then step by step, provide more info and break down the tasks to smaller and smaller units. The theory is that gradually, you will have a detailed, complete set of steps to perform the task. Let’s continue our breakdown… Copyright 2004, Richard T. Vannoy II

Initialize() 3/28/2017 From the previous slides, what needs to be declared here as places to store something? Copyright 2004, Richard T. Vannoy II

3/28/2017 Copyright 2004, Richard T. Vannoy II

3/28/2017 In case the game is played again, we need to set the number of guesses to zero. Copyright 2004, Richard T. Vannoy II

3/28/2017 This is the complete flow chart for process(), but let’s start over and build each part of the chart with an explanation Copyright 2004, Richard T. Vannoy II

3/28/2017 First, we must have an input from the human, their guess. (We could verify this to be a number from 1 to 100 if desired.) Copyright 2004, Richard T. Vannoy II

Next, we must ask several questions.
3/28/2017 Next, we must ask several questions. If the human’s guess is less than the computer’s number, we print a hint that the guess is too low. Copyright 2004, Richard T. Vannoy II

3/28/2017 We’ll come back to “Too low” shortly, but let’s ask the next question: Is the human’s guess bigger than the computer’s number? Copyright 2004, Richard T. Vannoy II

3/28/2017 We’ll come back to “Too low” shortly, but let’s ask the next question: Is the human’s guess bigger than the computer’s number? Notice that we put “Yes” and “No” at the two outputs of the decision diamond to show the flow direction for each answer. Copyright 2004, Richard T. Vannoy II

If the guess was too high, we show the “Too high” hint.
3/28/2017 If the guess was too high, we show the “Too high” hint. Copyright 2004, Richard T. Vannoy II

3/28/2017 If the answer to both of these questions was “No”, what is the only possibility left? Copyright 2004, Richard T. Vannoy II

3/28/2017 If the guess was not too high, AND not too low, then the human guessed the computer’s number. Display the good news. Copyright 2004, Richard T. Vannoy II

So the game is over. Let’s exit.
3/28/2017 So the game is over. Let’s exit. (Which takes us to the “Play Again?” section of the code. Copyright 2004, Richard T. Vannoy II

3/28/2017 Now we need to go back and add the steps required after the “Too low” and “Too high” messages are displayed. Copyright 2004, Richard T. Vannoy II

3/28/2017 In either case, the human made the wrong guess, so add one to the number of guesses taken. Copyright 2004, Richard T. Vannoy II

3/28/2017 Did the human take too many guesses without getting the correct number? Copyright 2004, Richard T. Vannoy II

3/28/2017 If the human took too many guesses, time to exit and go to “Play Again?” Copyright 2004, Richard T. Vannoy II

3/28/2017 And finally, if the human has any guesses left, send them back to guess again. Copyright 2004, Richard T. Vannoy II

Now, You Finish Up the Chart
3/28/2017 And lastly, the “Play Again?” section needs to ask the question: Do you want to play again? (Input the human’s answer.) If the answer is “Yes” direct the flowchart back to the beginning of the program. If the answer is “No” direct the flowchart to “End”. You make the flowchart for this part. Copyright 2004, Richard T. Vannoy II

3/28/2017 Copyright 2004, Richard T. Vannoy II

Download ppt "Writing Pseudocode And Making a Flow Chart A Number Guessing Game"

Similar presentations