Escolar Documentos
Profissional Documentos
Cultura Documentos
Chapter 4 - Arrays
Outline
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
Introduction
Arrays
Declaring Arrays
Examples Using Arrays
Passing Arrays to Functions
Sorting Arrays
Case Study: Computing Mean, Median and Mode Using Arrays
Searching Arrays: Linear Search and Binary Search
Multiple-Subscripted Arrays
Thinking About Objects: Identifying a Class's Behaviors
4.1
Introduction
Arrays
Structures of related data items
Static entity - same size throughout program
A few types
4.2
Arrays
Array
Consecutive group of memory locations
Same name and type
c[ 5 2 ] == c[ 3 ] == c[ x ]
2000 Prentice Hall, Inc. All rights reserved.
4.2
Name
that
this
same
Arrays
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
4.3
Declaring Arrays
Name
Type of array
Number of elements
Examples
int c[ 10 ];
float hi[ 3284 ];
int b[ 100 ], x[ 27 ];
4.4
Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
Outline
int main()
{
2. Define loop
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int i = 0; i < 10; i++ )
cout << setw( 7 ) << i << setw( 13 ) << n[ i ] <<
endl;
return 0;
}
Element
Value
0
32
1
27
2
64
3
18
4
95
5
14
6
90
7
70
8
60
9
37
2000 Prentice Hall, Inc. All rights reserved.
Program Output
3
4
int main()
const int x;
1. Initialize const
variable
// Error: x must be initialized
7
8
x = 7;
9
10
Outline
// Error:
2. Attempt to modify
variable
return 0;
11 }
Fig04_07.cpp:
Error E2304 Fig04_07.cpp 6: Constant variable 'x' must be
initialized in function main()
Error E2024 Fig04_07.cpp 8: Cannot modify a const object in
function main()
*** 2 errors in Compile ***
Program Output
4.4
Strings
Arrays of characters
All strings end with null ('\0')
Examples:
char string1[] = "hello";
char string1[] = { 'h', 'e', 'l', 'l', 'o',
'\0 };
Outline
int main() {
char string1[ 20 ], string2[] = "string
literal";
Inputted
strings are
cout << "Enter a string: ";
cin >> string1;
cout << "string1 is: " << string1
<< "\nstring2 is: " << string2
<< "\nstring1 with spaces between
separated by
2. Print strings
whitespace characters.
"there" stayed in the
2.1 Define loop
buffer.
characters is:\n";
}
Enter a
string1
string2
string1
H e l l
string1
1. Initialize strings
Program Output
10
11
4.5
12
4.5
Function prototype:
void modifyArray( int b[], int arraySize );
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
31
1. Define function to
take in arrays
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
void modifyArray( int [], int );
void modifyElement( int );
Outline
// appears strange
No parameter names
int main()
function prototype.
{
const int arraySize = 5;
int i, a[ arraySize ] = { 0, 1, 2, 3, 4 };
in
13
32
33
34
35
36
37
38
39
40
41
42
modifyElement( a[ 3 ] );
cout << "The value of a[3] is " << a[ 3 ] << endl;
43
44
45 }
46
47
48
49
50
51
Outline
return 0;
52
b[ j ] *= 2;
The value of a[3] is 6
53 }
Value in modifyElement is 12
54
55 // In function modifyElement, "e" is a local The
copyvalue
of of a[3] is 6
56 // array element a[ 3 ] passed from main.
57 void modifyElement( int e )
58 {
59
cout << "Value in modifyElement is "
60
<< ( e *= 2 ) << endl;
61 } 2000 Prentice Hall, Inc. All rights reserved.
14
Outline
Program Output
15
16
4.6
Sorting Arrays
Sorting data
Important computing application
Virtually every organization must sort some data
Massive amounts must be sorted
17
4.6
Sorting Arrays
Example:
Original: 3 4 2 6 7
Pass 1:
3 2 4 6 7
Pass 2:
2 3 4 6 7
Small elements "bubble" to the top
4.8
Binary search
Can only be used on sorted arrays
Compares middle element with key
If equal, match found
If key < middle, repeat search through the first half of the array
If key > middle, repeat search through the last half of the array
18
19
4.9
Multiple-Subscripted Arrays
Column 0
a[ 0 ][ 0 ]
Column 1
a[ 0 ][ 1 ]
Column 2
a[ 0 ][ 2 ]
Column 3
a[ 0 ][ 3 ]
Row 1
a[ 1 ][ 0 ]
a[ 1 ][ 1 ]
a[ 1 ][ 2 ]
a[ 1 ][ 3 ]
Row 2
a[ 2 ][ 0 ]
a[ 2 ][ 1 ]
a[ 2 ][ 2 ]
a[ 2 ][ 3 ]
Column subscript
Initialize
Array name
Row subscript
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
2000 Prentice Hall, Inc. All rights reserved.
20
4.9
Multiple-Subscripted Arrays
cout << b( 0, 1 );
Will try to call function b, causing a syntax error
Outline
// number
number of
//
of students
exams
1. Initialize variables
1.1 Define functions to
take double scripted
arrays
21
2. Call functions
minimum, maximum,
and average
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Outline
2. Call functions
minimum, maximum,
and average
3. Define functions
22
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Outline
3. Define functions
23
[1]
68
87
90
[2]
86
89
86
[3]
73
78
81
Lowest grade: 68
Highest grade: 96
The average grade for student 0 is 76.00
The average grade for student 1 is 87.50
The average grade for student 2 is 81.75
Outline
Program Output
24
4.7
Median
Number in middle of sorted list
1, 2, 3, 4, 5 (3 is median)
Mode
25
int main()
{
const int responseSize = 99;
int frequency[ 10 ] = { 0 },
response[ responseSize ] =
{ 6, 7, 8, 9, 8, 7, 8, 9,
7, 8, 9, 5, 9, 8, 7, 8,
6, 7, 8, 9, 3, 9, 8, 7,
7, 8, 9, 8, 9, 8, 9, 7,
6, 7, 8, 7, 8, 7, 9, 8,
7, 8, 9, 8, 9, 8, 9, 7,
5, 6, 7, 2, 5, 3, 9, 4,
7, 8, 9, 6, 8, 7, 8, 9,
7, 4, 4, 2, 5, 3, 8, 7,
4, 5, 6, 1, 6, 5, 7, 8,
8, 9,
7, 8,
8, 7,
8, 9,
9, 2,
5, 3,
6, 4,
7, 8,
5, 6,
7 };
Outline
26
1. Function prototypes
1.1 Initialize array
2. Call functions mean,
median, and mode
Outline
3. Define function
mean
}
void mean( const int answer[], int arraySize )
{
int total = 0;
cout << "********\n
Mean\n********\n";
27
68
69
70
71
72
73
74
75
76
77
Outline
3.1 Define function
median
3.1.1 Sort Array
3.1.2 Print middle
element
78 }
79
80 void mode( int freq[], int answer[], int size )
81 {
82
83
84
Mode\n********\n";
85
86
87
88
89
90
91
92
93
94
95
28
96
97
98
99
";
100
101
102
103
modeValue = rating;
104
105
106
107
108
109
110
111
112
113
114
<< " which occurred " << largest << " times." << endl;
115 }
116
117 void bubbleSort( int a[], int size )
118 {
119
120
int hold;
2000 Prentice Hall, Inc. All rights reserved.
Outline
29
121
Outline
122
123
124
125
if ( a[ j ] > a[ j + 1 ] ) {
126
hold = a[ j ];
127
a[ j ] = a[ j + 1 ];
128
a[ j + 1 ] = hold;
129
130 }
131
132 void printArray( const int a[], int size )
133 {
134
135
136
if ( j % 20 == 0 )
137
138
139
140
30
********
Mean
********
The mean is the average value of the data
items. The mean is equal to the total of
all the data items divided by the number
of data items (99). The mean value for
this run is: 681 / 99 = 6.8788
********
Median
********
The unsorted
6 7 8 9 8 7
6 7 8 9 3 9
6 7 8 7 8 7
5 6 7 2 5 3
7 4 4 2 5 3
The sorted
1 2 2 2 3
5 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9
array
8 9 8
8 7 8
9 8 9
9 4 6
8 7 5
array
3 3 3
6 6 6
7 7 7
8 8 8
9 9 9
of responses is
9 7 8 9 5 9 8 7
7 7 8 9 8 9 8 9
2 7 8 9 8 9 8 9
4 7 8 9 6 8 7 8
6 4 5 6 1 6 5 7
is
4 4
6 6
7 7
8 8
9 9
4
7
7
8
9
4
7
7
8
9
4
7
7
8
9
5
7
8
8
9
5
7
8
8
9
5
7
8
8
9
5
7
8
8
9
8
7
7
9
8
7
8
5
7
7
8
9
3
8
5
7
8
8
9
5
7
8
8
9
5
7
8
8
Outline
4. Program Output
31
********
Mode
********
Response
Outline
Frequency
Histogram
5
1
0
1
5
2
0
2
5
1
1
*
2
3
***
3
4
****
4
5
*****
5
8
********
6
9
*********
7
23
***********************
8
27
***************************
9
19
*******************
The mode is the most frequent value.
For this run the mode is 8 which occurred 27 times.
Program Output
32
33