lecture14 Arrays

lecture14 Arrays - CMPT 128 Introduction to Computing...

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

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, January 2007 CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128 January 2007 2 Simple and Composite Variables We have studied simple variables A simple variable describes a single value A simple variable has an identifier A simple variable has a type that describes the properties of the value of the variable, the permissible operations for the variable, and the representation of the variable in computer memory We can also have composite variables These variables describe a group of values Arrays: all values in the group have the same type Structures: different values in the group can have different types
Background image of page 2
© Janice Regan, CMPT 128 January 2007 3 Composite Variables composite variables describe a group of values 1 dimensional arrays or variables of a particular type (all entries must have the same type) multi dimensional arrays or variables of a particular type (all entries must have the same type) Structures containing groups of variables that may be of different types Composite variables can Used as arguments of functions Used as return values of functions
Background image of page 3

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128 January 2007 4 One-Dimensional (1-D) Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element of an array is accessed using the array name and an index or subscript The name of the array is the address of the first element and the subscript is the offset In C++, the subscripts always start with 0 and increment by 1
Background image of page 4
© Janice Regan, CMPT 128 January 2007 5 Declaration of a 1-D array An array is defined using a declaration statement. type array_name[size]; allocates memory for size elements subscript of first element is 0 subscript of last element is size-1 size must be a constant
Background image of page 5

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128 January 2007 6 Example Array Declaration in C++ int list[10]; allocates memory for 10 integer variables. Ten adjacent locations in memory are allocated subscript of first element is 0 subscript of last element is 9 C++ does not perform any bounds checking on arrays list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[9] list[8]
Background image of page 6
© Janice Regan, CMPT 128 January 2007 7 Example Array Declaration in C ++ double v2[6]; allocates memory for 6
Background image of page 7

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

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

This note was uploaded on 05/18/2010 for the course CMPT 128 taught by Professor Regan during the Spring '08 term at Simon Fraser.

Page1 / 26

lecture14 Arrays - CMPT 128 Introduction to Computing...

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

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