Arrays

ASSIGNMENT #10

ONE DIMENSIONAL ARRAYS: Part 1


 * Background**

Thus far in your exploration of Turing, you have been limited to three data types for single variables: real, integer, and string. There are, however, other data types. In particular, we are interested in the **array** type which allows us to g**roup single variables into a data structure**. If you are dealing with a lot of similar items of information, say phone numbers, composed in a list: One could store a series of phone numbers each assigned to a separate variable... phone1, phone2, phone3, etc.... By using an array, however, you can declare phone as an array type variable:

var phone: array 1 .. 50 of string

By doing so, you have declared memory locations for 50 phone numbers, all at once, and all with the same identifier. The different locations are identified as phone(1), phone (2), etc. Thus the entire list can be read into an array and treated as if it were a single variable. You will note that you must declare whether the array will be composed of real, integer, or string values.

Initializing an Array

Just as variables may have initial values assigned in their declaration statements, so to may arrays:

var phone: array 1..3 of string := init ("555-1111", "444-1234", "232-3434")

Loading an Array

Aside from the init function, you may also use a conditional or counted loop to load data into an array.

var x: array 1..5 of int var c:int:=0 loop c:=c+1 x(c):= c exit when c=5 end loop

or

var x: array 1..5 of int for c:1..5 x(c):= c end for

Displaying data stored in an Array

Suppose an array has already been loaded up, as in the examples above. To display the data, one could do this:

for q: 1..5 put x(q) end for

You could also do the same thing using a conditional loop:

var m:int:=0 loop m:=m+1 put x(m) exit when c=5 end loop

Task 1) Modify your string manipulation assignment to allow a user to input more than one word or letter at a time.

2) Create a program which will generate a rotisserie hockey league of 6 teams. Each team is to have 10 players not including a goalie (let's assume its 1937). The program will also generate the games played, goals, assists and penalty minutes earned by each player the previous year. It will award a value to each player based on his/her stats ranging from one to ten. The program will then conduct the draft. No team may have a total player value of over 80 nor may their be a difference between any two team player value of more that 35. The program is to be menu driven and must output each team list, the stats for each team, and the total player values for that team