Professional Documents
Culture Documents
Two-Dimensional Array
Slide 7- 3
The definition:
int tests[5];
allocates the following memory:
first
element
second
element
third
element
fourth
element
fifth
element
Slide 7- 4
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)
Slide 7- 5
Array Terminology
Slide 7- 6
Size Declarators
Slide 7- 7
subscripts:
0
Slide 7- 9
subscripts:
0
Slide 7- 10
Slide 7- 11
(Program Continues)
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 7- 12
Here are the contents of the hours array, with the values
entered by the user in the example output:
Slide 7- 13
Slide 7- 14
Slide 7- 15
Slide 7- 16
Default Initialization
Slide 7- 17
Slide 7- 19
Slide 7- 20
Slide 7- 21
Slide 7- 22
Off-By-One Errors
Slide 7- 23
Array Initialization
Slide 7- 25
Slide 7- 26
Slide 7- 27
17
15
11
Slide 7- 28
Slide 7- 29
Slide 7- 31
Array Assignment
To copy one array to another,
Dont try to assign one array to the other:
newTests = tests; // Won't work
Slide 7- 32
Slide 7- 33
Slide 7- 34
Slide 7- 35
Slide 7- 36
Slide 7- 37
Partially-Filled Arrays
Slide 7- 38
Comparing Arrays
Slide 7- 39
Slide 7- 41
Slide 7- 42
(Program Continues)
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 7- 43
Slide 7- 44
Slide 7- 45
Slide 7- 47
Slide 7- 48
Slide 7- 49
Slide 7- 50
Two-Dimensional Arrays
Slide 7- 52
Two-Dimensional Array
Representation
const int ROWS = 4, COLS = 3; int
exams[ROWS][COLS];
columns
r
o
w
s
exams[0][0]
exams[0][1]
exams[0][2]
exams[1][0]
exams[1][1]
exams[1][2]
exams[2][0]
exams[2][1]
exams[2][2]
exams[3][0]
exams[3][1]
exams[3][2]
Slide 7- 53
Slide 7- 54
Slide 7- 55
Slide 7- 56
2D Array Initialization
{92, 97} };
84
78
92
97
Slide 7- 57
Two-Dimensional Array as
Parameter, Argument
Slide 7- 58
Slide 7- 59
Slide 7- 60
Slide 7- 61
Slide 7- 62
Slide 7- 63
Slide 7- 64
Slide 7- 65
Slide 7- 66
Array of Strings
Slide 7- 68
Slide 7- 69
Slide 7- 70
Slide 7- 72
Slide 7- 74
Declaring Vectors
Slide 7- 75
Slide 7- 76
Slide 7- 77
Description
at(elt)
capacity()
maxelts =
Returns the maximum number of
elements a vector can store without vec1.capacity();
allocating more memory
Reverse the order of the elements vec1.reverse();
in a vector
reverse()
Example
resize
(elts,val)
vec1.resize(5,0);
swap(vec2)
vec1.swap(vec2);
Slide 7- 78