pascal programming arrays and string type. pascal programming n the array n what if you had a list...

10
Pascal Programming Arrays and String Type

Upload: emory-neal

Post on 17-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

Arrays and String Type

Page 2: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

The Array What if you had a list of 100 values you

wanted to store. That would require 100 variables—an unwieldy solution.

Instead, Pascal provides an array. The array consists of a two-part name.

Part 1 stays constant and part 2 changes. continue . . .

Page 3: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal programming

Array declaration and syntax . . .– type

• SmallArray = array[1 .. 5] of integer;

var

Score: SmallArray

The type after the of is the component type

All five variables have this type.

The variables are called indexed variables.

continue . . .

Page 4: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

Inside the square brackets –array [ 1 .. 5]– is the index or index expression or subscript.

The value of an indexed variable becomes an element or value of the array. Score := 1

The array is declared just like other types. – type

• array [index_type] of component type

The index_type can be an ordinal or subrange.

Component_type may be any Pascal type.

Page 5: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

Once an array type has been declared . . . a particular array is declared just like other variables.

In summary . . .– An array variable creates the locations– An array value occurs in each used location.– An element of the array will be any value in it.– The array index identifies locations and values in

an array.– An indexed variable identifies names the location.

Page 6: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

An array illustrates the structured type. Up to now, we have used simple types except string. Pascal programmers may created compound types

called structured type. In Turbo Pascal, the size of the array must be

declared. Thus, we must declare the largest anticipated size for the array.

Partially filled arrays present problems. We solve the problem with a variable Last that marks the last location filled with a value.

Page 7: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

A move through the array can be accomplished with a for loop. This is called sequential access.

The alternative is random access. When data is read in in sequence the

array fills from beginning to end. Random access allows data to be placed at an indexed location.

Page 8: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

Turbo Strings A string may be thought of as an array

of char.– Var

• Caption : string [20];

Caption := ‘Seize the day’

We read into array[11] p . . . The meaning changes. continue . . .

Page 9: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

Procedures and functions can use type string. . . But the string needs to be declared as a type. Then, the string type performs like an array.

String[20] is a definition but . . . String20 =string [20] is a declaration of type.

Functions, delivering a value, cannot return to an array be it can to a string type.

Page 10: Pascal Programming Arrays and String Type. Pascal Programming n The Array n What if you had a list of 100 values you wanted to store. That would require

Pascal Programming

Turbo Pascal provides a variety of predefined functions and procedures to manipulate string type.

Included are . . .• Length returns the length of a subset of string.• Pos identifies the position of a value.• Upcase returns an upper case char.• Delete eliminates defined values.• Copy copies a subset of string.