CHAP12 - Chapter 12 Arrays Dale/Weems/Headington 1 Chapter...

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

View Full Document Right Arrow Icon
1 Chapter 12 Arrays Dale/Weems/Headington
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Chapter 12 Topics Declaring and Using a One-Dimensional Array Passing an Array as a Function Argument Using const in Function Prototypes Using an Array of struct or class Objects Using an enum Index Type for an Array Declaring and Using a Two-Dimensional Array Two-Dimensional Arrays as Function Parameters Declaring a Multidimensional Array
Background image of page 2
3 C++ Data Types C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Structured Data Type A structured data type is a type that stores a collection of individual components with one variable name and allows individual components to be stored and retrieved
Background image of page 4
5 Declare variables to store and total 3 blood pressures int bp1, bp2, bp3; int total; 4002 4000 4004 bp2 bp1 bp3 cin >> bp1 >> bp2 >> bp3; total = bp1 + bp2 + bp3;
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 What if you wanted to store and total 1000 blood pressures? int bp[ 1000 ] ; // declares an array of 1000 int values bp[0] bp[1] bp[2] . . . . bp[999] 5000 5002 5004 5006 . . . .
Background image of page 6
7 One-Dimensional Array Definition An array is a structured collection of components (called array elements), all of the same data type , given a single name , and stored in adjacent memory locations. The individual components are accessed by using the array name together with an integral valued index in square brackets. The index indicates the position of the component within the collection.
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Another Example Declare an array called temps which will hold up to 5 individual float values. float temps[5]; // declaration allocates memory temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 number of elements in the array indexes or subscripts Base Address
Background image of page 8
9 Declaration of an Array the index is also called the subscript in C++, the first array element always has subscript 0. The second array element has subscript 1, etc. the base address of an array is its beginning address in memory SYNTAX DataType ArrayName [ConstIntExpression];
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Yet Another Example Declare an array called name which will hold up to 10 individual char values. char name[10]; // declaration allocates memory number of elements in the array name[0] name[1] name[2] name[3] name[4] . . . . . name[9] 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 Base Address
Background image of page 10
11 Assigning Values to Individual Array Elements float temps[ 5 ] ; // allocates memory for array int m = 4 ; temps[ 2 ] = 98.6 ; temps[ 3 ] = 101.2 ; temps[ 0 ] = 99.4 ; temps[ m ] = temps[ 3 ] / 2.0 ; temps[ 1 ] = temps[ 3 ] - 1.2 ; // what value is assigned? temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 99.4 ? 98.6 101.2 50.6
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 What values are assigned? float
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/13/2011 for the course CSC 140 taught by Professor Lebre during the Spring '04 term at Moraine Valley Community College.

Page1 / 65

CHAP12 - Chapter 12 Arrays Dale/Weems/Headington 1 Chapter...

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

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