Académique Documents
Professionnel Documents
Culture Documents
Array
Indexed variables
Subscripted variables
Elements of the array
Declaring an Array
Format
Name
Type of array
Any data type
Number of elements (array size)
type arrayName[ arraySize ];
Example
int c[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
Array
Subscript
Array
Name
that
this
same
A 12-element
Array named c
of array (Note
all elements of
array have the
name, c)
c[0]
-45
c[1]
c[2]
c[3]
72
c[4]
1543
c[5]
-89
c[6]
c[7]
62
c[8]
-3
c[9]
c[10]
6453
c[11]
78
Array
same as
c[3]
For loop
Example
First index is 0
Initializing Arrays
Example
int children[3] = { 2,
12,
1 };
Is equivalent to:
int children[3];
children[0] = 2;
children[1] = 12;
children[2] = 1;
11
12
Initializing Arrays
Example:
13
Initializing Arrays
14
Example program
15
C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using std::setw;
int main()
{
int n[ 10 ];
16
Output
17
Example program
18
C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
// use initializer list to initialize array n
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
cout << "Element" << setw( 13 ) << "Value" << endl;
Output
20
Example
Example program
22
C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
23
Output
24
Reading assignment
Book
Assignment
Example program
26
C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Output
28
int myArray[ 24 ];
myFunction( myArray, 24 );
Pass array myArray to function myFunction
Array size is usually passed
Useful to iterate over all elements
Example
Function prototype
void modifyArray( int b[], int arraySize );
void modifyArray( int [], int );
this prototype can also be written as
anyVariableName);
Ignored by compiler
33
Example program
35
C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
C++ code
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
array element by
value; the original cannot be
modified.
// output value of a[ 3 ]
cout << "The value of a[3] is " << a[ 3 ] << endl;
return 0; // indicates successful termination
} // end main
37
C++ code
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
38
Output
39
Output description
a is reprinted in main
Const Modifier
Examples
42
43
}
compute_average has no constant array parameter
44
Reading assignment
Figure 4.15
Returning An Array
46
Sorting Arrays
Sorting data
Sorting Arrays
Example:
Original: 3 4 2 7 6
Pass 1:
3 2 4 6 7 (elements exchanged)
Pass 2:
2 3 4 6 7
Pass 3:
2 3 4 6 7 (no changes needed)
Pass 4:
2 3 4 6 7
Pass 5:
2 3 4 6 7
Small elements "bubble" to the top (like 2 in this
example)
48
Sorting Arrays
Swapping variables
int x = 3, y = 4;
y = x;
x = y;
What happened?
Solution
int x = 3,
temp = x;
x = y;
y = temp;
y = 4, temp = 0;
// temp gets 3
// x gets 4
// y gets 3
49
Example program
50
C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
C++ code
27
28
29
30
31
32
33
34
35
36
37
38
39 40
41
42
43
44
45
46
47
48
49
50 }
52
Output
53
Sorting Arrays
It is easy to program
The drawback is
54