Learn to Program – Stage 3 Psuedocode (Arrays)


In today’s post, I want to discuss arrays and give you a little insight into how to use them in pseudocode.

Arrays:

Arrays are variables.  You can look at arrays in quite a few different ways.  You can look at them as a table (like an Excel Spreadsheet or a tic-tac-toe board), or you can look at them like multiple copies of the same variable.  Either way, hopefully a graphical representation will help.

For the representation, I’m going to use two different variable names.  cupOfCoffee and graphCoordinates.  cupOfCoffee will be a one-dimensional array, and graphCoordinates will be a two-dimensional array.

cupOfCoffee1 = 0
cupOfCoffee2 = 0
cupOfCoffee3 = 0
cupOfCoffee4 = 0

is the same as saying cupOfCoffee[4] = 0. 

Now for the two-dimensional array (this holds true for multi-dimensional arrays too—just more sets of numbers):

graphCoordinates[0,0] = 0
graphCoordinates[0,1] = 0
graphCoordinates[1,0] = 0
graphCoordinates[1,1] = 0

is the same as graphCoordinates[2][2] = 0.  And it would look like this, if you printed it out:

00
00

The important thing to remember with arrays is that the computer considers 0 to be the starting number of an index.

Another way to look at the two-dimensional array is like this:

graphCoordinates[0,0] = 1
graphCoordinates[0,1] = 2
graphCoordinates[1,0] = 3
graphCoordinates[1,1] = 4

and would be represented like this graphCoordinates[2][2] = {1,2,3,4} (this is also how you would list one-dimensional arrays that have values other than 0).  If you printed the array out, it would look like this:

12
34

Arrays are used to hold groups of related data.  Such as if you have four cups of coffee, you could put the total ounces that are in each cup.  Then as you drink one of the cups (it doesn’t have to be the first or last one), you can change the number to reflect the new level—without having to change any other cup.

In pseudocode, you will declare an array like you declare any other variable.

For a numeric array, it would be

num arrayname[index] = 0 (or {listed values}).

For a String, it would be

String arrayname[index] = “” (or {list of values in “’s separated by a comma}).

The other types will be the same.  The “index” is the total number of values you want to store.

Putting values into an array can be done using a FOR statement, an input statement, or even with case statements.  I will demonstrate with FOR statements here:

num MAXSIZE= 4
num array[MAXSIZE]= 0
FOR i = 0 TO MAXSIZE-1
     array[i] = i
ENDFOR

The reason I have the MAXSIZE-1 in there is because you may have four spaces in the array, but the computer has indexed them as 0, 1, 2, 3 (where you’re thinking 1,2,3,4).  So remember when dealing with arrays, you have to account for that difference between “Human indexing” and “Computer Indexing”.

In the FOR Statement above, if you changed the line array[i] = i to PRINT array[i], it would print the array.

In my next post, I will describe methods and how to use them in Pseudocode.  Thank you for reading these posts and if there is anything that you want me to describe in more detail, please leave me a comment or send me an e-mail to sales at patscomputerservices dot com (at=@ dot=.).

Have a great day:)
Patrick.

Leave a comment

Your email address will not be published. Required fields are marked *