Pseudocode
Pseudocode
Pseudocode
Summer 2012
Instructor: Hassan Khosravi
Guessing game
1. Tell the user to pick a secret number between 1 and 100.
2. The smallest possible number is 1; the largest possible is 100.
3. Make a guess that is halfway between the smallest and largest (round
down if necessary).
4. Ask the user if your guess is too large, too small or correct.
5. If they say you’re correct, you win and the game is over.
6. If they say your guess is too small, the smallest possible number is
now the guess plus one.
7. If they say your guess is too large, the largest possible number is now
the guess minus one.
8. Unless you guessed correctly, go back to step 3.
1.2
How many guess are required if your number is 33?
Can you think of a number where the algorithm in Figure 1.2 will make
7 guesses?
Can you think of a number where the algorithm in Figure 1.2 will make
8 guesses?
What is “legitimate input” for this algorithm? What happens if the user
enters something else?
1.3
Pseudocode
you need a way to describe the algorithms that you are going to
implement. This is often done with pseudocode
1.4
Storing Information
loops
Write command
Write command
Round down
If statement
1.5
Pseudocode
Storing Information:
Set variable to value
Set x to 1
Set y to “hello”
Set z to True
Read Command: sets the value of the variable to what is read from
the terminal
Read x
1.6
Example
Read two numbers and return their average
1.7
Example
Read the height of a person in meters and output their height in inches
1 meter = 39.37 inch
1.8
Example
Read the height of a person in meters and output their height in feet
1 feet = 12 inch
1.9
Round down
set y to X
If x is 2.3 then y=2
If x -2.3 then y = -3
Round up
Set y to X
If x is 2.3 then y=3
If x is -2.3 then y=-2
1.10
Example
Read the height of a person in meters and output their height in feet and inches
1. write"Enter your height in meters"
2. read height_in_meters
3. set height_in_inches to height_in_meters * 39.37
4. set feet to height_in_inches/12
5. round down feet to nearest integer
6. set inches to height_in_inches - (feet *12)
7. Round down inches
8. write "you are"
9. write feet, inches
10. write "tall“
Let’s try 1.80
height in meters: 1.80
height in inches 70.866
height in feet 5.09 5
inches 10.866 10
you are 5 10 tall
1.11
If statement
The most common way to make decisions is by using the if statement.
If Command
If statement then
Do some stuff
Else
Do some other stuff
The else part is optional and can be omitted.
Read X
If X < 2 then
Write X “is smaller than two”
Else
Write X “is bigger or equal to two”
1.12
Read a number, if it is odd then write odd, if it is even write it is even
1.13
What do you think of Pseudocode and the examples
A: They are very easy and the pace of the class is slow
B: They are understandable and the pace is right
C: I am finding the examples a little hard to follow. The pace of the
class is too fast
D: I just don’t get Pseudocode
1.14
Read a number between 1 and 10. try to guess it? If you guess it
correctly say you win, else say you lose.
1.15
Question
What would be the output if you input 13
1. Write “How old are you”
2. if age <= 2 then
3. write “you fly for free”
4. Else if 2 < age < 13 then
5. write “you pay the kids rate‘”
6. Else then
7. write 'you pay regular adult fare’
A: No output
B: you fly for free
C: you pay the kids rate
D: you pay regular adult fare
E: You get an error
The Value for age is undefined, so it doesn’t make sense to compare it.
You will get an error
1.16
Read three numbers and return their maximum
1.17
Definite Iteration: for loops
We need to be able to execute the same code several times
The for loop
The for loop can be used when you know ahead of time how many
times you want to execute some code
1.19
Examples
Compute the average of 10 numbers
1. set sum to 0
2. for i equal to each number from 1 to 10
3. read num
4. set sum to sum + num
5. average = sum/10
6. write average
1.20
Examples
Read a value and determine whether it is prime or not
1.21
Examples
Compute the first n values in the Fibonacci series.
Fibonacci series are the numbers in the following integer
sequence:
0,1,1,2,3,5,…
Fn = Fn-1 + Fn-2
F0 = 0, F1 =1
1.22
1. set smaller to 0
2. set bigger to 1
3. write smaller
4. write bigger
5. do this for i equal to each number from 1 to n-2\
6. newnum = smaller + bigger
7. write newnum
8. smaller = bigger
9. bigger= newnum
Trace the code to see what the 8th number in the series would be
A: 10
B:8
C:13
D:21
E:none of the above
1.23
Indefinite Iteration: while loops
If you don’t know how many times you want the loop body to execute,
the for loop is hard to work with.
While a statement holds do
Some stuff
You could write this with a for loop if you analyze it carefully
1.24
Example
Calculate the sum of some positive numbers, when you are finished
with your numbers give -1 as your number to terminate the program
1.25
Example
Enter a value n. Find the smallest number k where k2 is bigger than n
1. write"Enter a value"
2. read n
3. k=0
5. K=K+1
6. write k
1.26
If we put num1 as 13 and num2 as 3, what would be the output
1. write"Enter two numbers"
2. read num1,num2
3. num3=0
4. while num1>= num2
5. num1 = num1 - num2
6. num3 = num3+1
7. write num3, num1
A: num3 = 2, num1=2
B: num3 = 4, num1=1
C: num3=3, num1 = 5
D: num3=0, num1=0
E none of the Above
1.27