Presentation on theme: "1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II A Number Guessing Game."— Presentation transcript:
1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II A Number Guessing Game
2 Learning Objectives To read a word problem and create: Correct Pseudocode, and An accurate flow chart
3 The Problem: 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
4 The Solution: One, organized way to approach this problem uses the acronym D.I.P.O. D = Declare I = Input P = Process O = Output
5 D.I.P.O. 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.
6 D = Declare For the number guessing game we will need someplace to store… the number the computer picks. (Well call this variable number) the number of guesses made by the human. (Well call this variable howMany) The number the human enters as a guess. (And call this variable guess)
7 I = Input The input for this program is easy… The humans guess is the only input
8 P = Process The process for this program is easy… Read the humans guess from the console Compare the guess to the computers number Route to Guess again (with a hint) or Game over depending on human guess
9 O = Output 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?
10 The Chicken or the Egg? Which comes first? The pseudocode? or The flow chart? Actually, it doesnt matter…. Do one or the other first, or Do them both, side-by-side, at the same time.
11 The Pseudocode Because of this PowerPoint format, it would be cumbersome to do both together, so Ill 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.
12 The Pseudocode 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?
13 Modules/Subroutines 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.
14 Modules Here is what the big picture flow chart looks like in Visio:
15 Modules Next, lets try to write some pseudocode to match each of these shapes. (Look at the previous slides and apply what you know here.)
16 Modules Initialize: (We need to store) integer number (1-100) integer guess (1-100) integer howMany (1-7)
17 Modules 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)
18 Modules Play Again: If YES, return to start If NO, end program
19 Complete Pseudocode 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
20 Whats Next? 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
21 Top Down? 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. Lets continue our breakdown…
22 Initialize() From the previous slides, what needs to be declared here as places to store something?
24 In case the game is played again, we need to set the number of guesses to zero.
25 This is the complete flow chart for process(), but lets start over and build each part of the chart with an explanation
26 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.)
27 Next, we must ask several questions. If the humans guess is less than the computers number, we print a hint that the guess is too low.
28 Well come back to Too low shortly, but lets ask the next question: Is the humans guess bigger than the computers number?
29 Well come back to Too low shortly, but lets ask the next question: Is the humans guess bigger than the computers number? Notice that we put Yes and No at the two outputs of the decision diamond to show the flow direction for each answer.
30 If the guess was too high, we show the Too high hint.
31 If the answer to both of these questions was No, what is the only possibility left?
32 If the guess was not too high, AND not too low, then the human guessed the computers number. Display the good news.
33 So the game is over. Lets exit. (Which takes us to the Play Again? section of the code.
34 Now we need to go back and add the steps required after the Too low and Too high messages are displayed.
35 In either case, the human made the wrong guess, so add one to the number of guesses taken.
36 Did the human take too many guesses without getting the correct number?
37 If the human took too many guesses, time to exit and go to Play Again?
38 And finally, if the human has any guesses left, send them back to guess again.
39 Now, You Finish Up the Chart And lastly, the Play Again? section needs to ask the question: Do you want to play again? (Input the humans 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.