Copyright © Cay S. Horstmann 2015
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
i = 0 max = 0 imax = 0 while i < a.length j = i + 1 while j < a.length and a[i] == a[j] j++ if j - i > max max = j - i imax = i i = jWith the array
a = [4, 4, 3, 2, 2, 2, 5, 5]
, trace this algorithm. That is, take out a sheet of paper, make a table with columns for all the variables, and update them as the algorithm proceeds. The driver marks the current line, and the scribe updates the table. Don't erase the old values, but write the new values below the old ones.
If you can't trace it, it's not pseudocode.
We want to compute the area of a polygon recursively, by cutting it in two polygons, each with half the number of vertices. (We assume that the polygon is convex. That means, that it has no corners with angle > 180 degrees, or, more technically, that, if any two points p and q are contained in the polygon, so is the line joining them.)
Assuming that the points are stored in an array list p
, here is the pseudocode:
area(p)
n = p.size()
if n == 3
return triangle area of p[0], p[1], p[2]
else
k = n / 2
p1 = new array list
for i = 0 ... k
add p[i] to p1
p2 = new array list
for i = k to p.size() - 1
add p[i] to p2
add p[0] to p2
return area(p1) + area(p2)
Now complete this class. What is your code?
area(p)
n = p.size()
if n == 3
return triangle area of p[0], p[1], p[2]
else
Divide p into two polygons p1, p2, each of size n / 2
return area(p1) + area(p2)
What mistakes can you make in the vague step Divide p into two polygons p1, p2, each of size n / 2? Describe at least two mistakes.
If there are multiple ways of coding it (with different behavior), it's not pseudocode.
a
, e
, i
, count
, str
. Nothing wrong with short variable names. Consider this example:
Initialize an index to loop over the array While the index is not at the end of the array Check if the element at the index is the specific string If so, increment the counterReword this pseudocode so that it names all variables
++
operator, method calls, and so on. Rewrite the pseudocode from the preceding steps to do that. Also, use Java style if ...
statements, and not “check if ... if so”.int
, int[]
, ArrayList<Integer>
, etc.) That's another form of wordiness, and one should just drop them. If it's not clear from context that something is an array, you can always call the variable arr
or array
. Take this example:
minimumPositionAfter(a, r, c)
int[] temp = elements r,c;
int min = 21;
for loop int row =0 row < a[r] length iterate row
for loop int column = 0 column < a[c] length iterate column
if a[r][c] < min
min=a[r][c];
temp[0] = r;
temp[1] = c;
return temp;
Rewrite it without the types. While you are at it, drop the semicolons. Also, change the for
loops to the form
for variable in start...endAnd do something about those variable names.
r
and c
are used for two different purposes: The method parameters and the loop variables. Keep the meaning of the code intact--don't fix it.a
, starting at a[r][c]
. There are two obvious errors in the pseudocode. Fix them.x = last element of ais easier to write (and read) than
x = a.get(a.size() - 1)Of course, the assumption is that a reasonably competent programmer can quickly and unambigously translate such simple actions into Java. For example, what is this in Java?
ch = last character of str
sum = sum of all elements in a
i = position of first negative element in a
i = position of first $ in str
salary = characters in str with commas removed, as a double
i = 0 max = 0 imax = 0 while i < a.length j = i + 1 while a[i] == a[j] j++ if j - i > max max = j - i imax = i i = j + 1Simulate this code with the sequence
1 1 2 2 2 3There are two errors in the pseudocode. Find and fix them.
n = p.size() k = n / 2 p1 = new array list for i = 0 ... k add p[i] to p1 p2 = new array list for i = k + 1 to p.size() - 1 add p[i] to p2Trace this with
p = [c0, c1, c2, c3, c4, c5, c6, c7]
from the picture at the top of this page. What are you getting for p1
and p2
?p1
the red polygon? Is p2
the green polygon?minimumPositionAfter(a, r, c)
result = [r, c]
min = a[r][c]
for row = r ... #rows in a - 1
for col = c ... #cols in a - 1
if a[row][col] < min
min = a[row][col]
result = [row, rol]
return result
Simulate this pseudocode where a is the array
2 4 9 6 3 5 1 8 7and r and c are both 2. What result do you get?
You can save many hours of debugging code by “wasting” a few minutes writing and debugging pseudocode.
Description Quantity Unit Price Microwave Oven 12 39.95 Toaster 4 19.95 Stand Mixer 1 109.95We might start out with this high-level description:
discard first line of in for l in lines of in Extract quantity and unit price from l... total += quantity * unit price
Note the ... behind the second line. That's an indication that this line needs further refinement—it is still too vague.
Demonstrate that the line is too vague. Describe two different strategies (with possibly different semantics) in which one can execute “Extract quantity and unit price from the line...”
In this section, write pseudocode for some common programming problems. Be sure to understand and follow these syntax rules.
a
of integers, find the length of the i
th “run”, i.e. the i
th sequence of repeated elements. For example, in the array 1 1 2 2 2 3
, the first run has length 2, the second run length 3, the third run length 1.
lengthOfRun(a, i)
...
"Toaster Oven", "4", "19.95"or, technically,
"\"Toaster Oven\", \"4\", \"19.95\""you should produce an array of three strings,
"Toaster Oven"
, "4"
, and "19.95"
a
of integers and an integer k
, return an array of all positions of the k
th-smallest element. For example, if a
is [1, 4, 7, 5, 0, 5, 6, 4, 4, 5]
and k
is 3, then return the array [1, 7, 8]
because the 3rd smallest element is 4, and it occurs at positions 2, 7, and 8.