The Computational Universe

COS 116 The Computational Universe Pseudocode Reference Pseudocode is a way to describe how to accomplish tasks using basic steps like those a computer might perform. In this week's lab, you saw how a form of pseudocode can be used to program the Scribbler robot. The Scribbler control panel has a point and click interface, but in the rest of the course you will write your own simple pseudocode to express computations. The advantage of pseudocode over plain English (as you saw in case of scribbler) is that it has a precise meaning. The exact syntax is not too important— what counts is expressing a computation clearly and precisely. You can use the handout below in all your homework and exams (even "closed-book" exams). • Variables In pseudocode you will need to use variables to store data. You can think of these as little boxes that hold a number, and you are allowed to look at what's in the box or replace its contents with something else. We call whatever is inside the box the value of the variable. An array is a shorthand way of naming a bunch of variables. If A is an array of length n, you can imagine it as n boxes lined up in a row. Then we write A[i] to refer to the i'th box. Here's a picture of what A might look like in memory: You can use arrays in pseudocode instructions the same way you use variables: x ← A[2] Sets x to the second value in the array A (here, 62.71) A[3] ← 2 Sets the third value in the array A to the value 2 (replacing 52.54) Sometimes you will use a variable to specify which element of the array you mean: y ← A[i] Sets y to the i'th array value Arrays can be multidimensional. While a one-dimensional array is like a list, a two-dimensional array is like a grid. If A is a two-dimensional array, A[i][j] refers to the value in row i, column j of the grid.