lecture06-1 - Today: aggregate types 1: collections of...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Today: aggregate types 1: collections of homogeneous (like-typed) things 2: Array basics 3: Arrays as arguments 4: Array pitfalls 5: Pointers So far, we've worked with two sorts of data types. The first are "atoms": types with singleton values. For example: something of type "int" holds one "integer". Something of type "char" holds one character. The second are "aggregate" types: types that hold "sets" of values, where each value is of some type. When all values have the same type, we call it a "homogeneous" aggregate, otherwise it is "heterogeneous." These are sometimes also called "container" types, because they contain other types. The two we've seen so far, in the context of project 2, are lists and trees: each holds a collection of integers. In contrast, C++ has a structure called an *array*. An array is a fixed-sized, indexed, homogeneous aggregate type (a collection of items, all of the same type.) To declare and define an array of four integers (the same size as our list above), we would say the following: int array[4]; For comparison purposes, let's also declare and define an integer, foo: int foo; The environment that we get when we do this is: +--------------------------+ | +----+ | | foo | | | | +----+ | | | | / +----+ | | / | | | | / +----+ | | / | | | | array | +----+ | | \ | | | | \ +----+ | | \ | | | | \ +----+ | +--------------------------+ Question: what are the contents of "array" after this declaration? Just as one can initialize an integer, one can also initialize the contents of an array. Since we aren't initializing one value, though, we need some sort of notation to specify a set of numbers. We do that with braces, like so: int foo = 7; int array[4] = { 1, 2, 3, 4 }; And the corresponding environment would look like this: +--------------------------+ | +----+ | | foo | 7 | | | +----+ | | | | / +----+ | | / | 1 | | | / +----+ | | / | 2 | | | array | +----+ | | \ | 3 | | | \ +----+ | | \ | 4 | | | \ +----+ | +--------------------------+ This is called a "static initializer". Of course, arrays---just like lists---are composed of individual elements. How does one go about using these elements? Unlike lists, one can access each element of the array individually and directly, through its index. The index of the first array element is zero, the next is one, and so on. So, we can name the individual elements of array, like so: +------------------------------+ | +----+ | | foo | 7 | | | +----+ | | | | / +----+ | | /array[0] | 1 | | | / +----+ | | / array[1] | 2 | | | array | +----+ | | \ array[2] | 3 | | | \ +----+ | | \array[3] | 4 | | | \ +----+ | +------------------------------+ Each individual element is used just like a regular int, so all of the following are legal: array[1] = 6; array[1]++; array[0] = array[1]; after which, the environment is: +--------------------------+ | +----+ | | foo | 7 | | | +----+ | | | | / +----+ | | / | 7 | | | / +----+ | | / | 7 | | | array | +----+ | | \ | 3 | | | \ +----+ | | \ | 4 | | | \ +----+...
View Full Document

Page1 / 12

lecture06-1 - Today: aggregate types 1: collections of...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online