Programming in c++ Arrays Dale/eems/Headington
1 Arrays
Programming in C++ Chapter 12 Topics One-Dimensional Arrays s Using const in Function Prototypes s Using an Array of struct or class Obiects Using an enum Index Type for an Array s Special Kinds of Array Processing s& Two-Dimensional Arrays s Passing Two-Dimensional Arrays as Arguments Another Way of Defining Two-Dimensional Arrays Multidimensional Arrays
2 Chapter 12 Topics ❖One-Dimensional Arrays ❖Using const in Function Prototypes ❖Using an Array of struct or class Objects ❖Using an enum Index Type for an Array ❖Special Kinds of Array Processing ❖Two-Dimensional Arrays ❖Passing Two-Dimensional Arrays as Arguments ❖Another Way of Defining Two-Dimensional Arrays ❖Multidimensional Arrays
Programming in C++ C++ Data Types simple structured integral enum floating array struct union class char short int long bool float double long double address pointer reference
3 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double
Programming in C++ Structured Data Type A structured data type is a type that o stores a collection of individual components with one variable name o and allows individual components to be stored and retrieved
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
Programming in C++ Declare variables to store and total 3 blood pressures int bp1, bp2, bp3 int total 4000 4002 4004 bp1 bp2 bp3 cin >>bp1 > bp2 > bp3 total s bp1+ bp2+ bp3;
5 Declare variables to store and total 3 blood pressures int bp1, bp2, bp3; int total; 4000 4002 4004 bp1 bp2 bp3 cin >> bp1 >> bp2 >> bp3; total = bp1 + bp2 + bp3;
Programming in C++ What if you wanted to store and total 1000 blood pressures? int bp[ 10001 M/ declares an array of 1000 int values 5000 5002 5004 5006 bp[o] bp[1l bp[2 bp999]
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 . . .
Programming in C++ 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
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
Difference Between Array and++ Programming in C Struct or class o As array is a structure, an array differs from struct or class in two fundamental was 1. An array is a homogeneous data structure whereas structs and classes are heterogeneous types 2. A component of an array is accessed by its position in the structure, whereas a component of a struct or class is accessed by an identifie
8 Difference Between Array and Struct or class ❖As array is a structure, an array differs from struct or class in two fundamental was: 1. An array is a homogeneous data structure,whereas structs and classes are heterogeneous types. 2. A component of an array is accessed by its position in the structure, whereas a component of a struct or class is accessed by an identifier
Programming in C++ Another Example o Declare an array called temps which will hold up to 5 individual float values number of elements in the array float temps 5]; //declaration allocates memory Base Address 7000 7004 7008 7012 7016 tem ps[o temps[1] temps[2 temps[3 temps[41 indexes or subscripts 9
9 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
Programming in C++ Declaration of an Array oo the index is also called the subscript o 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 Data Type ArrayName [ConstIntExpression 10
10 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];