Pseudocode examples from Dave Mulkey

advertisement
output "Welcome"
loop for COUNT from 1 to 5
output COUNT
end loop
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
=== EZ Pcode =================================
This tool is intended to help teachers prepare
Pseudocode that is consistent with the new
IB Computer Science standards for the 2014 syllabus.
This tool is a "quick and dirty hack" that
translates Pcode into equivalent JavaScript.
This is done with simple search and replace commands,
so it is not very sophisticated and it's relatively
easy to write code that won't run properly.
One warning - the keyword NOT should never be used
as part of a variable name, e.g. NOTHING would be
a bad variable name, as it will translate to !HING.
Suggestions for improvement are welcome send an eMail to the author at Dave_Mulkey@fis.edu
// === Mice ============
// JavaScript has loops, including for loops
// like Java and Basic and other languages.
// This example uses loops for a poem.
loop for A from 1 to 2
output "Three blind mice"
end loop
loop for B from 3 to 4
output "See how they run"
end loop
output "They all ran up to the farmer's wife"
output "She cut off their tails with a carving knife"
output "Did you ever see such a sight in your life, as"
C = 5
loop while C < 20
output "Three blind mice"
C = C*2
end loop
//
//
//
//
//
=== Money =================
This program converts money from EUROS into
several other currencies. Then it uses IF commands
to make some decisions.
EUROS = 50.00
POUNDS = 0.8*EUROS
DOLLARS = EUROS / 0.75
YEN = EUROS * 90
output EUROS + " EUR"
output YEN + " Yen"
if YEN > 1000 then
output "That is a lot of Yen"
end if
output POUNDS + " BP"
if POUNDS < 100 then
output "That is a small number of Pounds"
end if
output "$" + DOLLARS
if DOLLARS = 100 then
output "BINGO"
end if
//==== Common Factors ===============================
// This program prints all the common factors of A and B.
// This would be useful for reducing fractions.
A = 28
B = 42
output "Common factors of " + A + " and " + B
loop for C from 1 to B
if (A mod C = 0) AND (B mod C = 0) then
output C
end if
end loop
//
//
//
//
//
//
//
//
=== Table of Values for Math Function ===
This program uses a simple loop to produce a table
of x,y values for a math function.
Notice that the LOOP FOR command can only count by 1,
so the program divides by 2 to count by 0.5 each time.
Dividing by 2 only works if you divide by 2.0,
because C / 2 would do integer division, ignoring
the fractional result.
output "X , Y"
loop for C from 0 to 10
X = C / 2.0
Y = 3*X*X - 7*X + 2
output X + " , " + Y
end loop
//
//
//
//
//
//
//
//
=== Translate ==============
This program knows a few German words.
If you choose an English word from the list,
it will tell you the matching German word.
It knows the following words in German:
hello goodbye stop
input ENGLISH
if ENGLISH = "hello" then
GERMAN = "guten Tag"
else if ENGLISH = "goodbye" then
GERMAN = "auf Wiedersehen"
else if ENGLISH = "stop" then
GERMAN = "halt"
else
GERMAN = "???"
end if
output "English = " + ENGLISH
output "German = " + GERMAN
//=== Elapsed Minutes ======================
// This program inputs a STARTING time and a FINISH time.
// Then it calculates the number of elapsed minutes
// between those two times. For example:
//
START_HOURS = 8
// START_MINUTES = 30
//
END_HOURS = 10
//
END_MINUTES = 10
// MINUTES = (10-8)*60 + (10-30) = 100
input START_HOURS
input START_MINUTES
input END_HOURS
input END_MINUTES
if START_HOURS > 23 OR START_MINUTES > 59 then
output "Start time is not valid"
else if END_HOURS > 23 OR END_MINUTES > 59 then
output "Times are not valid"
else
MINUTES = (END_HOURS - START_HOURS)*60 + (END_MINUTESSTART_MINUTES)
output "Elapsed time = " + MINUTES + " minutes"
end loop
//==== Date Validation ================================
// This code inputs a date, using numbers for months,
// and decides whether the date is VALID or not.
input MONTH
input DAY
input YEAR
output MONTH + "/" + DAY + "/" + YEAR
if YEAR mod 4 = 0 then
FEBMAX = 29
else
FEBMAX = 28
end if
M = MONTH
D = DAY
if M < 1 OR M > 12 then
output "Month is not valid"
else if D < 1 OR D > 31 then
output "Day is not valid"
else if D = 31 AND (M = 4 OR M = 6 OR M = 9 OR M = 11) then
output "That month does not have 31 days"
else if M = 2 AND D > FEBMAX then
output "February only has " + FEBMAX + " days"
else
output "Date is valid"
end if
//==== Adding Up Numbers =========================
// This program adds up a lot of numbers :
// 1 + 2 + 3 + ... + max
MAX = 10
SUM = 0
loop for COUNT from 0 to MAX
output COUNT
SUM = SUM + COUNT
end loop
output "Total = " + SUM
//
//
//
//
//
//
==== Arrays ===================
Arrays work just like Java and C++,
except that they can contain any data type.
This program uses the Sieve of Eratosthenes
to produce a list of PRIMES below 100.
NUMS = new Array()
loop for N from 1 to 100
NUMS[N] = 0
end for
loop for P from 2 to 50
N = P * 2
loop while N <= 100
NUMS[N] = 1
N = N + P
end loop
end loop
output "These are the PRIME numbers under 100"
loop for N from 2 to 100
if NUMS[N] = 0 then
output N
end if
end for
//==== Binary Search ==========
// This program uses a Binary Search algorithm to search
// for an ID number, and then prints the corresponding name.
// Notice that div does not work properly in this tool,
// so Math.floor is used to truncate the average of LOW and HIGH.
ID = [1001,1002,1050,1100,1120,1180,1200,1400]
NAME =
["Apple","Cherry","Peach","Banana","Fig","Grape","Olive","Mango"]
input TARGET
LOW = 0
HIGH = 7
FOUND = -1
loop while FOUND = -1 AND LOW <= HIGH
MID = div( LOW + HIGH , 2 )
// should be (LOW + HIGH) div 2
// but (A div B) doesn't work correctly
if ID[MID] = TARGET then
FOUND = MID
else if TARGET < ID[MID] then
HIGH = MID - 1
else
LOW = MID + 1
end if
end while
if FOUND >= 0 then
output TARGET + ":" + NAME[FOUND]
else
output TARGET + " was not found"
end if
//==== Reverse Array ===============================
// This program is supposed to reverse the order
// of the names in the array. Trace the program
// and predict what it will print after the
// loop executes.
NAMES = ["Robert","Boris","Brad","George","David"]
N = 5
K = 0
// the number of elements in the array
// this is the first index in the array
loop while K < N - 1
TEMP = NAMES[K]
NAMES[K] = NAMES[N - K - 1]
NAMES[N - K - 1] = TEMP
K = K + 1
end loop
loop for C from 0 to N-1
output NAMES[C]
end loop
//==== Frequency Distribution ====================
// This program examines the values stored in an array.
// It counts the values in each range : 0..9, 10..19, ...
DATA = [17,20,23,29,33,42,60,61,75,75,90,99]
FREQS = [0,0,0,0,0,0,0,0,0,0]
loop for C from 0 to 11
VALUE = DATA[C]
loop for F from 0 to 9
if VALUE >= 10*F AND VALUE < 10*(F+1) then
FREQS[F] = FREQS[F] + 1
end if
end loop
end for
output "Data"
loop for D from 0 to 9
output DATA[D]
end for
output "Range : Frequency"
loop for F from 0 to 9
output F*10 + " - " + (F+1)*10 + " : " + FREQS[F]
end for
//==== Appointment List ===================================
// This program maintains a list of daily appointments in
// an array. The secretary types the name of a student and then the
time,
// like this : 1215 for the time 12:15, or 850 for the time 8:50.
// After each entry, the program prints a list of all appointments.
APPS = new Array()
NAME = ""
loop for T from 0 to 2400
APPS[T] = ""
end loop
loop while NAME <> "quit"
input NAME
input TIME
if TIME >= 0 AND TIME <= 2359 then
APPS[TIME] = NAME
end if
loop for T from 0 to 2400
if APPS[T] <> "" then
output T + " : " + APPS[T]
end if
end loop
output "=================="
end loop
//==== Checking Lists for Duplicates ====================
// This program compares two lists, stored in Arrays,
// checking for duplicate names that appear in both lists.
SOCCER = ["Al","Bobby","Carla","Dave","Ellen"]
BBALL = ["Lou","Dave","Hellen","Alan","Bobbie"]
output "The following appear in both lists"
loop for S from 0 to 4
loop for B from 0 to 4
if SOCCER[S] = BBALL[B] then
output SOCCER[S]
end if
end for
end for
//==== Cities Array =========================
// This program contains an array with a list of city names.
// It counts and prints all the names that start with "D".
CITIES = ["Athens","Berlin","Dallas","Denver","London","New
York","Rome"]
COUNT = 0
loop for C from 0 to 6
if firstLetter( CITIES[C] ) = "D" then
COUNT = COUNT + 1
output CITIES[C]
end if
end loop
output "That was " + COUNT + " cities"
method firstLetter(s)
return s.substring(0,1)
end method
//==== Cities Collection =========================
// This shows how to add names of students into
// a Collection, and then search the collection and
// print all the student names that begin with "D".
// It includes a METHOD that returns
// the first letter of a String.
NAMES = new Collection()
NAMES.add("Bob")
NAMES.add("Dave")
NAMES.add("Betty")
NAMES.add("Kim")
NAMES.add("Debbie")
NAMES.add("Lucy")
NAMES.resetNext()
loop while NAMES.hasNext()
NAME = NAMES.getNext()
if firstLetter(NAME) = "D" then
output NAME
end if
end loop
method firstLetter(s)
return s.substring(0,1)
end method
Download