Download Introduction to Programming in Turing First Edition

Transcript
Chapter 14 : Arrays and Other Data Types
243
put "Enter the array size : " ..
get size
var numbers : array 1 .. size of int
14.2 Manipulating Lists
Here is a program that reads a list of names and outputs the
list in reverse order.
% The "ReverseNames" program
% Reads a list of names and outputs in reverse
var howMany : int
put "How many names are there? " ..
get howMany
var name : array 1 .. howMany of string (20)
put "Enter ", howMany, " names, one to a line"
for i : 1 .. howMany
get name (i ) : *
end for
put "Here are the names in reverse order"
for decreasing i : howMany .. 1
put name (i )
end for
Notice that the variable name is an array of strings of
maximum length 20. This saves space since the default length of
strings is 256. The number of elements in the array is not known
until the program executes. At that time you tell it how many there
are. This is known as dynamic declaration of an array.
Here is an Execution window.
How many names are there?
4
Enter 4 names, one to a line
Mark Mendell
Steve Perelgut
Chris Stephenson