21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

14.15 Records (structures) 195<br />

Individual elements of the array are referenced by specifying the array name and an<br />

expression <strong>for</strong> each subscript, <strong>for</strong> example, table[2].<br />

The implementation of arrays in programming languages raises the following considerations:<br />

■ what restrictions are placed on the element type? For complete freedom of expression<br />

there should be no restrictions.<br />

■ valid indices should be any subrange of numbers (e.g. 2010 to 2020)<br />

■ at what time must the size of an array be known? The utility of arrays in a programming<br />

language is governed by the time (compile-time or run-time) at which<br />

the size of the array must be known.<br />

■ what operations may be applied to complete arrays? For example, it is very convenient<br />

to be able to carry out array assignment or comparison between compatible arrays<br />

using a single concise statement.<br />

■ are convenient techniques available <strong>for</strong> the initialization of arrays?<br />

The time at which a size must be specified <strong>for</strong> an array has implications on how the<br />

array may be used. In Java, as in most languages, the size of an array must be defined<br />

statically – the size and subscript ranges are required to be known at compile-time. This<br />

has the advantage of allowing the compiler to generate code automatically to check <strong>for</strong><br />

out-of-range subscripts. However, the disadvantage of this simple scheme is that, to<br />

allow the program to accommodate data sets of differing sizes, we would like to delay<br />

deciding the size of the array until run-time. Most languages provide arrays whose size<br />

is fixed at compile-time, so if variable size is needed, a dynamic data structure is the<br />

answer (see Chapter 15).<br />

SELF-TEST QUESTION<br />

14.5 Argue <strong>for</strong> and against the language making array subscripts start at 0.<br />

14.15 ● Records (structures)<br />

Data objects in problem domains are not always simply collections of homogeneous<br />

objects (same types). Rather, they are often collections of heterogeneous objects (different<br />

types). Although such collections can be represented using arrays, many programming<br />

languages provide a record data aggregate. Records (or structures as they are<br />

termed in C and C++) are generalizations of arrays where the elements (or fields) may<br />

be of different types and where individual components are referenced by (field) name<br />

rather than by position.<br />

For example, the C++ struct definition shown below describes in<strong>for</strong>mation relating<br />

to a time. Each object of type Time has three components named hour, minute<br />

and second.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!