Professional Documents
Culture Documents
Topics
8.1 Arrays Hold Multiple Values 8.2 Accessing Array Elements 8.3 Inputting and Displaying Array Contents 8.4 Array Initialization 8.5 Processing Array Contents 8.6 Using Parallel Arrays
Chapter 8 slide 2
Topics
8.7 The typedef Statement
8.8 Arrays as Function Arguments 8.9 Two-Dimensional Arrays 8.10 Vectors 8.13 Arrays of Structures 8.14 Arrays of Class Objects
Chapter 8 slide 3
Chapter 8 slide 4
first element
second element
third element
fourth element
fifth element
Chapter 8 slide 5
Array Terminology
In the definition int tests[5]; int is the data type of the array elements tests is the name of the array 5, in [5], is the size declarator. It shows the number of elements in the array. The size of an array is (number of elements) * (size of each element)
Chapter 8 slide 6
Array Terminology
The size of an array is:
the total number of bytes allocated for it (number of elements) * (number of bytes for each element)
Examples:
int tests[5] is an array of 20 bytes, assuming 4 bytes for an int long double measures[10]is an array of 80 bytes, assuming 8 bytes for a long double
Chapter 8 slide 7
Chapter 8 slide 8
Chapter 8 slide 10
Initial values used in order; cannot skip over elements to initialize noncontiguous range
Chapter 8 slide 14
Must use either array size declarator or initialization list at array definition
Chapter 8 slide 15
Strings
Can be processed using array name (entire string at once) or using subscripts(element at a time):
string city; cout << "Enter city name: "; cin >> city;
'S' city[0] 'a' 'l' 'e' 'm' city[1] city[2] city[3] city[4]
Chapter 8 slide 18
Chapter 8 slide 19
Example:
typedef unsigned int uint; uint tests[5]; // array of unsigned // ints
Chapter 8 slide 21
Uses of typedef
Used to make code more readable Can be used to create alias for array of a particular type:
typedef int program[8]; // program now names a data type // that is an array of 8 ints program prog1, prog2;// 2 arrays
Chapter 8 slide 22
To define a function that takes an array parameter, use empty [] for array argument:
void showScores(int []); // function prototype void showScores(int tests[]) // function header
Chapter 8 slide 23
Chapter 8 slide 26
Initialization at Definition
Two-dimensional arrays are initialized row-by-row:
int exams[2][2] = { {84, 78}, 84 78 {92, 97} };
92 97
Can omit inner { }, some initial values in row array elements without initial values will be set to 0 or NULL
Chapter 8 slide 29
Use empty [] for row, size declarator for column in prototype, header:
void getExams(int [][2], int); // prototype void getExams(int exams[][2], int rows) // header
Chapter 8 slide 30
Multi-Dimensional Arrays
Can define arrays with any number of dimensions:
short rectSolid(2,3,5); float timeGrid(3,4,3,4);
8.10 Vectors
Defined in the Standard Template Library (Chapter 15) Can hold values of any type:
vector<int> scores;
Automatically adds space as more is needed no need to determine size at definition Can use [] to access elements
Chapter 8 slide 33
Declaring Vectors
Vectors require vector header file Declare a vector:
vector<int> scores;
Use push_back member function to add element to a full array or to an array that had no defined size:
scores.push_back(75);
Chapter 8 slide 35
Arrays of Structures
Use array subscript to access a specific structure in the array Then, use dot operator to access members of structure:
cin >> class[25].studentID; cout << class[i].name << "has GPA " << class[i].gpa << endl;
Chapter 8 slide 38
Chapter 8 slide 42
Alternate Version of