Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Pseudocode And Making a Flow Chart A Number Guessing Game

Similar presentations


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

1 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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

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

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

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

39 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

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


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

Similar presentations


Ads by Google