40
1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II [email protected] A Number Guessing Game

1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II [email protected] A Number Guessing Game

Embed Size (px)

Citation preview

Page 1: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

1

Writing Pseudocode

And

Making a Flow ChartInstructor: Richard T. Vannoy II

[email protected]

A Number Guessing Game

Page 2: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

2

Learning Objectives

To read a word problem and create: Correct Pseudocode, and An accurate flow chart

Page 3: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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

Page 4: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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

Page 5: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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.

Page 6: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

6

D = Declare

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)

Page 7: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

7

I = Input

The input for this program is easy…

The human’s guess is the only input

Page 8: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

8

P = Process

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

Page 9: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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?”

Page 10: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

10

The Chicken or the Egg?

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.

Page 11: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

11

The Pseudocode

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.

Page 12: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

12

The Pseudocode

The “Big Picture”, meaning lacking details or specifics of the pseudocode would look like this.

Computer picks a numberHuman gets 7 guesses Possible responses:

- Too high - To low - Correct! You win! - Too many guesses. You lose.Play again?

Page 13: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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.

Page 14: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

14

Modules

Here is what the “big picture” flow chart looks like in Visio:

Page 15: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

15

Modules

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.)

Page 16: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

16

Modules

Initialize: (We need to store) integer number (1-100) integer guess (1-100) integer howMany (1-7)

Page 17: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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)

Page 18: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

18

Modules

Play Again:

If YES, return to start

If NO, end program

Page 19: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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

Page 20: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

20

What’s 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

Page 21: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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.

Let’s continue our breakdown…

Page 22: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

22

Initialize()

From the previous slides, what needs to be declared here as places to store something?

Page 23: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

23

Page 24: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

24

In case the game is played again, we need to set the number of guesses to zero.

Page 25: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

25

This is the complete flow chart for process(), but let’s start over and build each part of the chart with an explanation

Page 26: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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.)

Page 27: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

27

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.

Page 28: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

28

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?

Page 29: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

29

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.

Page 30: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

30

If the guess was too high, we show the “Too high” hint.

Page 31: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

31

If the answer to both of these questions was “No”, what is the only possibility left?

Page 32: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

32

If the guess was not too high, AND not too low, then the human guessed the computer’s number.

Display the good news.

Page 33: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

33

So the game is over. Let’s exit.

(Which takes us to the “Play Again?” section of the code.

Page 34: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

34

Now we need to go back and add the steps required after the “Too low” and “Too high” messages are displayed.

Page 35: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

35

In either case, the human made the wrong guess, so add one to the number of guesses taken.

Page 36: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

36

Did the human take too many guesses without getting the correct number?

Page 37: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

37

If the human took too many guesses, time to exit and go to “Play Again?”

Page 38: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

38

And finally, if the human has any guesses left, send them back to guess again.

Page 39: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

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 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.

Page 40: 1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II RVannoy@ITT-Tech.edu A Number Guessing Game

40