Escolar Documentos
Profissional Documentos
Cultura Documentos
Mean
Median
Mode
Example program
responseSize
Each of the responses is a number from 1 to 9
Program computes
The mean
median and
mode of the 99 values
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
std::cout;
std::endl;
std::fixed;
std::showpoint;
#include <iomanip>
using std::setw;
using std::setprecision;
void
void
void
void
void
int main()
{
const int responseSize = 99; // size of array responses
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
C++ code
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
C++ code
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
C++ code
96 // determine most frequent response
97 void mode( int freq[], int answer[], int size )
98 {
99
int largest = 0; // represents largest frequency
100
int modeValue = 0; // represents most frequent response
101
102
cout << "\n********\n Mode\n********\n";
103
104
// initialize frequencies to 0
105
for ( int i = 1; i <= 9; i++ )
106
freq[ i ] = 0;
107
108
// summarize frequencies
109
for ( int j = 0; j < size; j++ )
110
++freq[ answer[ j ] ];
111
112
// output headers for result columns
113
cout << "Response" << setw( 11 ) << "Frequency"
114
<< setw( 19 ) << "Histogram\n\n" << setw( 55 )
115
<< "1 1 2 2\n" << setw( 56 )
116
<< "5 0 5 0 5\n\n";
117
C++ code
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 }
// output results
for ( int rating = 1; rating <= 9; rating++ ) {
cout << setw( 8 ) << rating << setw(The
11 )mode is the value that
<< freq[ rating ] << "
";
occurs most often (has the
highest
value
in
// keep track of mode value and largest
fequency
value
if ( freq[ rating ] > largest ) {
largest = freq[ rating ];
modeValue = rating;
freq).
} // end if
// output histogram bar representing frequency value
for ( int k = 1; k <= freq[ rating ]; k++ )
cout << '*';
cout << '\n'; // begin new line of output
} // end outer for
// display the mode value
cout << "The mode is the most frequent value.\n"
<< "For this run the mode is " << modeValue
<< " which occurred " << largest << " times." << endl;
// end function mode
C++ code
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
10
C++ code
166 // output array contents (20 values per row)
167 void printArray( const int a[], int size )
168 {
169
for ( int i = 0; i < size; i++ ) {
170
171
if ( i % 20 == 0 ) // begin new line every 20 values
172
cout << endl;
173
174
cout << setw( 2 ) << a[ i ];
175
176
} // end for
177
12
Searching Arrays
13
Linear Search
Inefficient for
Large 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
16
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
// display results
if ( element != -1 )
cout << "Found value in element " << element << endl;
else
cout << "Value not found" << endl;
return 0; // indicates successful termination
} // end main
// compare key to every element of array until location is
// found or until end of array is reached; return subscript of
// element if key or -1 if key not found
int linearSearch( const int array[], int key, int sizeOfArray )
{
for ( int j = 0; j < sizeOfArray; j++ )
if ( array[ j ] == key ) // if found,
return j;
// return location of key
return -1; // key not found
} // end function linearSearch
17
Output
18
Binary Search
Binary Search
Example
10
= 1024
Example program
Following program performs the binary
search of a sorted array
The program is written in such a way that
when it is executed
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
22
C++ code
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
23
C++ code
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
middle element
24
C++ code
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}
return -1; // searchKey not found
} // end function binarySearch
// print header for output
void printHeader( int size )
{
cout << "\nSubscripts:\n";
// output column heads
for ( int j = 0; j < size; j++ )
cout << setw( 3 ) << j << ' ';
cout << '\n'; // start new line of output
25
C++ code
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
26
C++ code
119
120
// display other elements in subarray
121
else
122
cout << setw( 3 ) << b[ m ] << ' ';
123
124
cout << endl; // start new line of output
125
126 } // end function printRow
27
Output
28
Output
29
Multiple-Subscripted Arrays
Double-Subscript arrays
Multiple-Subscripted Arrays
Row 0
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
Array name
Row subscript
Array of arrays
31
To initialize
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
1 and 2 initialize b[0][0] and b[0][1]
3 and 4 initialize b[1][0] and b[1][1]
Default of 0
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
32
Multiple-Subscripted Arrays
Outputs 0
0 is element
b[0][1] of array b
cout << b[ 0, 1 ];
Syntax error
33
Multiple-Subscripted Arrays
Function prototypes
Multiple-Subscripted Arrays
35
Multiple-Subscripted Arrays
Example 2
following nested for structure determines the total of all the
elements in array a
total = 0;
for (row = 0; row < 3; row++)
for( column = 0; column < 4; column++)
total += a[ row ][ column ];
36
Example program
37
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
38
C++ code
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
39
Output
40
Example program
Following program is
Quiz2
Student0 95
85
Student1
80
89
.
.
41
Example program
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
std::cout;
std::endl;
std::fixed;
std::left;
#include <iomanip>
using std::setw;
using std::setprecision;
const int students = 3;
const int exams = 4;
// number of students
// number of exams
// function prototypes
int minimum( int [][ exams ], int, int );
int maximum( int [][ exams ], int, int );
double average( int [], int );
void printArray( int [][ exams ], int, int );
43
C++ code
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int main()
{
// initialize student grades for three students (rows)
int studentGrades[ students ][ exams ] =
{ { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
{ 70, 90, 86, 81 } };
// output array studentGrades
cout << "The array is:\n";
printArray( studentGrades, students, exams );
// determine smallest and largest grade values
cout << "\n\nLowest grade: "
<< minimum( studentGrades, students, exams )
<< "\nHighest grade: "
<< maximum( studentGrades, students, exams ) << '\n';
cout << fixed << setprecision( 2 );
// calculate average grade for each student
for ( int person = 0; person < students; person++ )
cout << "The average grade for student " << person
<< " is "
<< average( studentGrades[ person ], exams )
<< endl;
44
C++ code
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
70
45
C++ code
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
46
C++ code
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
47
Output
48
We
about
49