Вы находитесь на странице: 1из 93

1

2007 Pearson Education, Inc. All rights reserved.


6
6
C Arrays
2
2007 Pearson Education, Inc. All rights reserved.
Now go, write it before them in a table,
and note it in a book.
Isaiah 30:8
To go beyond is as wrong as to fall short.
Confucius
Begin at the beginning,
and go on till you come to the end: then stop.
Lewis Carroll
3
2007 Pearson Education, Inc. All rights reserved.
OBJECTIVES
In this chapter you will learn:
To use the array data structure to represent lists
and tables of values.
To define an array, initialize an array and refer to
individual elements of an array.
To define symbolic constants.
To pass arrays to functions.
To use arrays to store, sort and search lists and
tables of values.
To define and manipulate multiple-subscripted
arrays.
4
2007 Pearson Education, Inc. All rights reserved.
6.1 Introduction
6.2 Arrays
6.3 Defining Arrays
6.4 Array Examples
6.5 Passing Arrays to Functions
6.6 Sorting Arrays
6.7 Case Study: Computing Mean, Median and Mode
Using Arrays
6.8 Searching Arrays
6.9 Multiple-Subscripted Arrays
5
2007 Pearson Education, Inc. All rights reserved.
6.1 Introduction
Arrays
Structures of related data items
Static entity same size throughout program
Dynamic data structures discussed in Chapter 12
6
2007 Pearson Education, Inc. All rights reserved.
6.2 Arrays
Array
Group of consecutive memory locations
Same name and type
To refer to an element, specify
Array name
Position number
Format:
arrayname[ position number ]
First element at position 0
n element array named c:
- c[ 0 ] , c[ 1 ] . . . c[ n 1 ]
7
2007 Pearson Education, Inc. All rights reserved.
Fig. 6.1 | 12-element array.
8
2007 Pearson Education, Inc. All rights reserved.
6.2 Arrays
Array elements are like normal variables
c[ 0 ] = 3;
pr i nt f ( " %d" , c[ 0 ] ) ;
Perform operations in subscript. If x equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
9
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.1
It is important to note the difference between
the seventh element of the array and array
element seven. Because array subscripts
begin at 0, the seventh element of the array
has a subscript of 6, while array element
seven has a subscript of 7 and is actually the
eighth element of the array. This is a source of
off-by-one errors.
10
2007 Pearson Education, Inc. All rights reserved.
Operators Associativity Type
[ ] ( ) left to right highest
++ - - !

( type) right to left unary
* / % left to right multiplicative
+ - left to right additive
< <= > >=

left to right relational
== ! = left to right equality
&& left to right logical AND
| | left to right logical OR
?: right to left conditional
= += - = *= / = %= right to left assignment
, left to right comma

Fig. 6.2 | Operator precedence.
11
2007 Pearson Education, Inc. All rights reserved.
6.3 Defining Arrays
When defining arrays, specify
Name
Type of array
Number of elements
ar r ayType ar r ayName[ number Of El ement s ] ;
Examples:
i nt c[ 10 ] ;
f l oat myAr r ay[ 3284 ] ;
Defining multiple arrays of same type
Format similar to regular variables
Example:
i nt b[ 100 ] , x[ 27 ] ;
12
2007 Pearson Education, Inc. All rights reserved.
6.4 Array Examples
Initializers
i nt n[ 5 ] = { 1, 2, 3, 4, 5 };
If not enough initializers, rightmost elements become 0
i nt n[ 5 ] = { 0 }
- All elements 0
If too many initializers, a syntax error occurs
C arrays have no bounds checking
If size omitted, initializers determine it
i nt n[ ] = { 1, 2, 3, 4, 5 };
5 initializers, therefore 5 element array
13
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 3: f i g06_03. c
2 i ni t i al i zi ng an ar r ay */
3 #i ncl ude <st di o. h>
4
5 / * f unct i on mai n begi ns pr ogr amexecut i on */
6 i nt mai n( voi d )
7 {
8 i nt n[ 10 ] ; / * n i s an ar r ay of 10 i nt eger s */
9 i nt i ; / * count er */
10
11 / * i ni t i al i ze el ement s of ar r ay n t o 0 */
12 f or ( i = 0; i < 10; i ++ ) {
13 n[ i ] = 0; / * set el ement at l ocat i on i t o 0 */
14 } / * end f or */
15
16 pr i nt f ( " %s%13s\ n", " El ement ", " Val ue" ) ;
17
18 / * out put cont ent s of ar r ay n i n t abul ar f or mat */
19 f or ( i = 0; i < 10; i ++ ) {
20 pr i nt f ( " %7d%13d\ n", i , n[ i ] ) ;
21 } / * end f or */
22
23 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
24
25 } / * end mai n */

Outline
f i g06_03. c
(1 of 2 )
for loop initializes each array
element separately
for loop outputs all array elements
14
2007 Pearson Education,
Inc. All rights reserved.

El ement Val ue
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0


Outline
f i g06_03. c
(2 of 2 )
15
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 4: f i g06_04. c
2 I ni t i al i zi ng an ar r ay wi t h an i ni t i al i zer l i st */
3 #i ncl ude <st di o. h>
4
5 / * f unct i on mai n begi ns pr ogr amexecut i on */
6 i nt mai n( voi d )
7 {
8 / * use i ni t i al i zer l i st t o i ni t i al i ze ar r ay n */
9 i nt n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
10 i nt i ; / * count er */
11
12 pr i nt f ( " %s%13s\ n", " El ement ", " Val ue" ) ;
13
14 / * out put cont ent s of ar r ay i n t abul ar f or mat */
15 f or ( i = 0; i < 10; i ++ ) {
16 pr i nt f ( " %7d%13d\ n", i , n[ i ] ) ;
17 } / * end f or */
18
19 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
20
21 } / * end mai n */

Outline
f i g06_04. c
(1 of 2 )
initializer list initializes all array
elements simultaneously
16
2007 Pearson Education,
Inc. All rights reserved.

El ement Val ue
0 32
1 27
2 64
3 18
4 95
5 14
6 90
7 70
8 60
9 37


Outline
f i g06_04. c
(2 of 2 )
17
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.2
Forgetting to initialize the elements of an
array whose elements should be initialized.
18
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.3
Providing more initializers in an array
initializer list than there are elements
in the array is a syntax error.
19
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 5: f i g06_05. c
2 I ni t i al i ze t he el ement s of ar r ay s t o t he even i nt eger s f r om2 t o 20 */
3 #i ncl ude <st di o. h>
4 #def i ne SI ZE 10 / * maxi mumsi ze of ar r ay */
5
6 / * f unct i on mai n begi ns pr ogr amexecut i on */
7 i nt mai n( voi d )
8 {
9 / * symbol i c const ant SI ZE can be used t o speci f y ar r ay si ze */
10 i nt s[ SI ZE ] ; / * ar r ay s has SI ZE el ement s */
11 i nt j ; / * count er */
12
13 f or ( j = 0; j < SI ZE; j ++ ) { / * set t he val ues */
14 s[ j ] = 2 + 2 * j ;
15 } / * end f or */
16
17 pr i nt f ( " %s%13s\ n", " El ement ", " Val ue" ) ;
18
19 / * out put cont ent s of ar r ay s i n t abul ar f or mat */
20 f or ( j = 0; j < SI ZE; j ++ ) {
21 pr i nt f ( " %7d%13d\ n", j , s[ j ] ) ;
22 } / * end f or */
23
24 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
25
26 } / * end mai n */

Outline
f i g06_05. c
(1 of 2 )
#define directive tells compiler to replace all
instances of the word SIZE with 10
SIZE is replaced with 10 by the
compiler, so array s has 10 elements
for loop initializes each array
element separately
20
2007 Pearson Education,
Inc. All rights reserved.

El ement Val ue
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20


Outline
f i g06_05. c
(2 of 2 )
21
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.4
Ending a #def i ne or #i ncl ude preprocessor
directive with a semicolon. Remember that
preprocessor directives are not C statements.
22
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.5
Assigning a value to a symbolic constant in
an executable statement is a syntax error.
A symbolic constant is not a variable. No
space is reserved for it by the compiler as with
variables that hold values at execution time.
23
2007 Pearson Education, Inc. All rights reserved.
Software Engineering Observation 6.1
Defining the size of each array as a symbolic
constant makes programs more scalable.
24
2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 6.1
Use only uppercase letters for symbolic
constant names. This makes these constants
stand out in a program and reminds you
that symbolic constants are not variables.
25
2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 6.2
In multiword symbolic constant names, use
underscores to separate the words for
readability.
26
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 6: f i g06_06. c
2 Comput e t he sumof t he el ement s of t he ar r ay */
3 #i ncl ude <st di o. h>
4 #def i ne SI ZE 12
5
6 / * f unct i on mai n begi ns pr ogr amexecut i on */
7 i nt mai n( voi d )
8 {
9 / * use i ni t i al i zer l i st t o i ni t i al i ze ar r ay */
10 i nt a[ SI ZE ] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45 };
11 i nt i ; / * count er */
12 i nt t ot al = 0; / * sumof ar r ay */
13
14 / * sumcont ent s of ar r ay a */
15 f or ( i = 0; i < SI ZE; i ++ ) {
16 t ot al += a[ i ] ;
17 } / * end f or */
18
19 pr i nt f ( " Tot al of ar r ay el ement val ues i s %d\ n", t ot al ) ;
20
21 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
22
23 } / * end mai n */

Tot al of ar r ay el ement val ues i s 383


Outline
f i g06_06. c
initializer list initializes all array
elements simultaneously
for loop adds each element of the
array to variable total
27
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 7: f i g06_07. c
2 St udent pol l pr ogr am*/
3 #i ncl ude <st di o. h>
4 #def i ne RESPONSE_SI ZE 40 / * def i ne ar r ay si zes */
5 #def i ne FREQUENCY_SI ZE 11
6
7 / * f unct i on mai n begi ns pr ogr amexecut i on */
8 i nt mai n( voi d )
9 {
10 i nt answer ; / * count er t o l oop t hr ough 40 r esponses */
11 i nt r at i ng; / * count er t o l oop t hr ough f r equenci es 1- 10 */
12
13 / * i ni t i al i ze f r equency count er s t o 0 */
14 i nt f r equency[ FREQUENCY_SI ZE ] = { 0 };
15
16 / * pl ace t he sur vey r esponses i n t he r esponses ar r ay */
17 i nt r esponses[ RESPONSE_SI ZE ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
18 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
19 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
20
21 / * f or each answer , sel ect val ue of an el ement of ar r ay r esponses
22 and use t hat val ue as subscr i pt i n ar r ay f r equency t o
23 det er mi ne el ement t o i ncr ement */
24 f or ( answer = 0; answer < RESPONSE_SI ZE; answer ++ ) {
25 ++f r equency[ r esponses [ answer ] ] ;
26 } / * end f or */
27

Outline
f i g06_07. c
(1 of 2 )
#define directives create
symbolic constants
frequency array is
defined with 11 elements
responses array is defined
with 40 elements and its
elements are initialized
subscript of frequency array is given
by value in responses array
28
2007 Pearson Education,
Inc. All rights reserved.
28 / * di spl ay r esul t s */
29 pr i nt f ( " %s%17s\ n", " Rat i ng", " Fr equency" ) ;
30
31 / * out put t he f r equenci es i n a t abul ar f or mat */
32 f or ( r at i ng = 1; r at i ng < FREQUENCY_SI ZE; r at i ng++ ) {
33 pr i nt f ( " %6d%17d\ n", r at i ng, f r equency[ r at i ng ] ) ;
34 } / * end f or */
35
36 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
37
38 } / * end mai n */

Rat i ng Fr equency
1 2
2 2
3 2
4 2
5 5
6 11
7 5
8 7
9 1
10 3


Outline
f i g06_07. c
(2 of 2 )
29
2007 Pearson Education, Inc. All rights reserved.
Good Programming Practice 6.3
Strive for program clarity. Sometimes it may
be worthwhile to trade off the most efficient
use of memory or processor time in favor of
writing clearer programs.
30
2007 Pearson Education, Inc. All rights reserved.
Performance Tip 6.1
Sometimes performance considerations
far outweigh clarity considerations.
31
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.6
Referring to an element outside the array bounds.
32
2007 Pearson Education, Inc. All rights reserved.
Error-Prevention Tip 6.1
When looping through an array, the array
subscript should never go below 0 and should
always be less than the total number of
elements in the array (size 1). Make sure the
loop-terminating condition prevents accessing
elements outside this range.
33
2007 Pearson Education, Inc. All rights reserved.
Error-Prevention Tip 6.2
Programs should validate the correctness of all
input values to prevent erroneous information
from affecting a programs calculations.
34
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 8: f i g06_08. c
2 Hi st ogr ampr i nt i ng pr ogr am*/
3 #i ncl ude <st di o. h>
4 #def i ne SI ZE 10
5
6 / * f unct i on mai n begi ns pr ogr amexecut i on */
7 i nt mai n( voi d )
8 {
9 / * use i ni t i al i zer l i st t o i ni t i al i ze ar r ay n */
10 i nt n[ SI ZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
11 i nt i ; / * out er f or count er f or ar r ay el ement s */
12 i nt j ; / * i nner f or count er count s *s i n each hi st ogr ambar */
13
14 pr i nt f ( " %s%13s%17s\ n", " El ement ", " Val ue", " Hi st ogr am" ) ;
15
16 / * f or each el ement of ar r ay n, out put a bar of t he hi st ogr am*/
17 f or ( i = 0; i < SI ZE; i ++ ) {
18 pr i nt f ( " %7d%13d ", i , n[ i ] ) ;
19
20 f or ( j = 1; j <= n[ i ] ; j ++ ) { / * pr i nt one bar */
21 pr i nt f ( " %c" , ' *' ) ;
22 } / * end i nner f or */
23
24 pr i nt f ( " \ n" ) ; / * end a hi st ogr ambar */
25 } / * end out er f or */
26
27 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
28
29 } / * end mai n */

Outline
f i g06_08. c
(1 of 2 )
nested for loop prints n[ i ]
asterisks on the ith line
35
2007 Pearson Education,
Inc. All rights reserved.

El ement Val ue Hi st ogr am
0 19 *******************
1 3 ***
2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *


Outline
f i g06_08. c
(2 of 2 )
36
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 9: f i g06_09. c
2 Rol l a si x- si ded di e 6000 t i mes */
3 #i ncl ude <st di o. h>
4 #i ncl ude <st dl i b. h>
5 #i ncl ude <t i me. h>
6 #def i ne SI ZE 7
7
8 / * f unct i on mai n begi ns pr ogr amexecut i on */
9 i nt mai n( voi d )
10 {
11 i nt f ace; / * r andomdi e val ue 1 - 6 */
12 i nt r ol l ; / * r ol l count er 1- 6000 */
13 i nt f r equency[ SI ZE ] = { 0 }; / * cl ear count s */
14
15 sr and( t i me( NULL ) ) ; / * seed r andom- number gener at or */
16
17 / * r ol l di e 6000 t i mes */
18 f or ( r ol l = 1; r ol l <= 6000; r ol l ++ ) {
19 f ace = 1 + r and( ) %6;
20 ++f r equency[ f ace ] ; / * r epl aces 26- l i ne swi t ch of Fi g. 5. 8 */
21 } / * end f or */

Outline
f i g06_09. c
(1 of 2 )
for loop uses one array to track
number of times each number is
rolled instead of using 6 variables
and a switch statement
37
2007 Pearson Education,
Inc. All rights reserved.
22
23 pr i nt f ( " %s%17s\ n", " Face", " Fr equency" ) ;
24
25 / * out put f r equency el ement s 1- 6 i n t abul ar f or mat */
26 f or ( f ace = 1; f ace < SI ZE; f ace++ ) {
27 pr i nt f ( " %4d%17d\ n", f ace, f r equency[ f ace ] ) ;
28 } / * end f or */
29
30 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
31
32 } / * end mai n */

Face Fr equency
1 1029
2 951
3 987
4 1033
5 1010
6 990


Outline
f i g06_09. c
(2 of 2 )
38
2007 Pearson Education, Inc. All rights reserved.
6.4 Array Examples
Character arrays
String f i r st is really a static array of characters
Character arrays can be initialized using string literals
char st r i ng1[ ] = " f i r st " ;
- Null character ' \ 0' terminates strings
- st r i ng1 actually has 6 elements
It is equivalent to
char st r i ng1[ ] ={ ' f ' , ' i ' , ' r ' , ' s' , ' t ' , ' \ 0' };
Can access individual characters
st r i ng1[ 3 ] i s char act er s
Array name is address of array, so & not needed for scanf
scanf ( " %s" , st r i ng2 ) ;
- Reads characters until whitespace encountered
- Be careful not to write past end of array, as it is possible to do so
39
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.7
Not providing scanf with a character array
large enough to store a string typed at the
keyboard can result in destruction of data in
a program and other runtime errors. This
can also make a system susceptible to worm
and virus attacks.
40
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 10: f i g06_10. c
2 Tr eat i ng char act er ar r ays as st r i ngs */
3 #i ncl ude <st di o. h>
4
5 / * f unct i on mai n begi ns pr ogr amexecut i on */
6 i nt mai n( voi d )
7 {
8 char st r i ng1[ 20 ] ; / * r eser ves 20 char act er s */
9 char st r i ng2[ ] = " st r i ng l i t er al "; / * r eser ves 15 char act er s */
10 i nt i ; / * count er */
11
12 / * r ead st r i ng f r omuser i nt o ar r ay st r i ng1 */
13 pr i nt f ( " Ent er a st r i ng: ") ;
14 scanf ( " %s", st r i ng1 ) ; / * i nput ended by whi t espace char act er */
15
16 / * out put st r i ngs */
17 pr i nt f ( " st r i ng1 i s: %s\ nst r i ng2 i s: %s\ n"
18 " st r i ng1 wi t h spaces bet ween char act er s i s: \ n",
19 st r i ng1, st r i ng2 ) ;
20
21 / * out put char act er s unt i l nul l char act er i s r eached */
22 f or ( i = 0; st r i ng1[ i ] ! = ' \ 0' ; i ++ ) {
23 pr i nt f ( " %c " , st r i ng1[ i ] ) ;
24 } / * end f or */
25
26 pr i nt f ( " \ n" ) ;
27
28 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
29
30 } / * end mai n */

Outline
f i g06_10. c
(1 of 2 )
string2 array is defined with one
element for each character, so 15
elements including null character /0
for loop prints characters of string1
array with spaces in between
41
2007 Pearson Education,
Inc. All rights reserved.

Ent er a st r i ng: Hel l o t her e
st r i ng1 i s: Hel l o
st r i ng2 i s: st r i ng l i t er al
st r i ng1 wi t h spaces bet ween char act er s i s:
H e l l o


Outline
f i g06_10. c
(2 of 2 )
42
2007 Pearson Education, Inc. All rights reserved.
Performance Tip 6.2
In functions that contain automatic arrays
where the function is in and out of scope
frequently, make the array st at i c so it is
not created each time the function is called.
43
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 11: f i g06_11. c
2 St at i c ar r ays ar e i ni t i al i zed t o zer o */
3 #i ncl ude <st di o. h>
4
5 voi d st at i cAr r ayI ni t ( voi d ) ; / * f unct i on pr ot ot ype */
6 voi d aut omat i cAr r ayI ni t ( voi d ) ; / * f unct i on pr ot ot ype */
7
8 / * f unct i on mai n begi ns pr ogr amexecut i on */
9 i nt mai n( voi d )
10 {
11 pr i nt f ( " Fi r st cal l t o each f unct i on: \ n" ) ;
12 st at i cAr r ayI ni t ( ) ;
13 aut omat i cAr r ayI ni t ( ) ;
14
15 pr i nt f ( " \ n\ nSecond cal l t o each f unct i on: \ n" ) ;
16 st at i cAr r ayI ni t ( ) ;
17 aut omat i cAr r ayI ni t ( ) ;
18
19 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
20
21 } / * end mai n */
22

Outline
f i g06_11. c
(1 of 4 )
44
2007 Pearson Education,
Inc. All rights reserved.
23 / * f unct i on t o demonst r at e a st at i c l ocal ar r ay */
24 voi d st at i cAr r ayI ni t ( voi d )
25 {
26 / * i ni t i al i zes el ement s t o 0 f i r st t i me f unct i on i s cal l ed */
27 st at i c i nt ar r ay1[ 3 ] ;
28 i nt i ; / * count er */
29
30 pr i nt f ( " \ nVal ues on ent er i ng st at i cAr r ayI ni t : \ n" ) ;
31
32 / * out put cont ent s of ar r ay1 */
33 f or ( i = 0; i <= 2; i ++ ) {
34 pr i nt f ( " ar r ay1[ %d ] = %d ", i , ar r ay1[ i ] ) ;
35 } / * end f or */
36
37 pr i nt f ( " \ nVal ues on exi t i ng st at i cAr r ayI ni t : \ n" ) ;
38
39 / * modi f y and out put cont ent s of ar r ay1 */
40 f or ( i = 0; i <= 2; i ++ ) {
41 pr i nt f ( " ar r ay1[ %d ] = %d ", i , ar r ay1[ i ] += 5 ) ;
42 } / * end f or */
43
44 } / * end f unct i on st at i cAr r ayI ni t */

Outline
f i g06_11. c
(2 of 4 )
static array is created only once, when
staticArrayInit is first called
45
2007 Pearson Education,
Inc. All rights reserved.
45
46 / * f unct i on t o demonst r at e an aut omat i c l ocal ar r ay */
47 voi d aut omat i cAr r ayI ni t ( voi d )
48 {
49 / * i ni t i al i zes el ement s each t i me f unct i on i s cal l ed */
50 i nt ar r ay2[ 3 ] = { 1, 2, 3 };
51 i nt i ; / * count er */
52
53 pr i nt f ( " \ n\ nVal ues on ent er i ng aut omat i cAr r ayI ni t : \ n" ) ;
54
55 / * out put cont ent s of ar r ay2 */
56 f or ( i = 0; i <= 2; i ++ ) {
57 pr i nt f ( " ar r ay2[ %d ] = %d ", i , ar r ay2[ i ] ) ;
58 } / * end f or */
59
60 pr i nt f ( " \ nVal ues on exi t i ng aut omat i cAr r ayI ni t : \ n" ) ;
61
62 / * modi f y and out put cont ent s of ar r ay2 */
63 f or ( i = 0; i <= 2; i ++ ) {
64 pr i nt f ( " ar r ay2[ %d ] = %d ", i , ar r ay2[ i ] += 5 ) ;
65 } / * end f or */
66
67 } / * end f unct i on aut omat i cAr r ayI ni t */

Outline
f i g06_11. c
(3 of 4 )
automatic array is recreated every time
automaticArrayInit is called
46
2007 Pearson Education,
Inc. All rights reserved.

Fi r st cal l t o each f unct i on:

Val ues on ent er i ng st at i cAr r ayI ni t :
ar r ay1[ 0 ] = 0 ar r ay1[ 1 ] = 0 ar r ay1[ 2 ] = 0
Val ues on exi t i ng st at i cAr r ayI ni t :
ar r ay1[ 0 ] = 5 ar r ay1[ 1 ] = 5 ar r ay1[ 2 ] = 5

Val ues on ent er i ng aut omat i cAr r ayI ni t :
ar r ay2[ 0 ] = 1 ar r ay2[ 1 ] = 2 ar r ay2[ 2 ] = 3
Val ues on exi t i ng aut omat i cAr r ayI ni t :
ar r ay2[ 0 ] = 6 ar r ay2[ 1 ] = 7 ar r ay2[ 2 ] = 8

Second cal l t o each f unct i on:

Val ues on ent er i ng st at i cAr r ayI ni t :
ar r ay1[ 0 ] = 5 ar r ay1[ 1 ] = 5 ar r ay1[ 2 ] = 5
Val ues on exi t i ng st at i cAr r ayI ni t :
ar r ay1[ 0 ] = 10 ar r ay1[ 1 ] = 10 ar r ay1[ 2 ] = 10

Val ues on ent er i ng aut omat i cAr r ayI ni t :
ar r ay2[ 0 ] = 1 ar r ay2[ 1 ] = 2 ar r ay2[ 2 ] = 3
Val ues on exi t i ng aut omat i cAr r ayI ni t :
ar r ay2[ 0 ] = 6 ar r ay2[ 1 ] = 7 ar r ay2[ 2 ] = 8


Outline
f i g06_11. c
(4 of 4 )
47
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.8
Assuming that elements of a local st at i c
array are initialized to zero every time the
function in which the array is defined is called.
48
2007 Pearson Education, Inc. All rights reserved.
6.5 Passing Arrays to Functions
Passing arrays
To pass an array argument to a function, specify the name of the
array without any brackets
i nt myAr r ay[ 24 ] ;
myFunct i on( myAr r ay, 24 ) ;
- Array size usually passed to function
Arrays passed call-by-reference
Name of array is address of first element
Function knows where the array is stored
- Modifies original memory locations
Passing array elements
Passed by call-by-value
Pass subscripted name (i.e., myAr r ay[ 3 ] ) to function
49
2007 Pearson Education, Inc. All rights reserved.
6.5 Passing Arrays to Functions
Function prototype
voi d modi f yAr r ay( i nt b[ ] , i nt ar r aySi ze ) ;
Parameter names optional in prototype
- i nt b[ ] could be written i nt [ ]
- i nt ar r aySi ze could be simply i nt
50
2007 Pearson Education, Inc. All rights reserved.
Performance Tip 6.3
Passing arrays by reference makes sense for
performance reasons. If arrays were passed
by value, a copy of each element would be
passed. For large, frequently passed arrays,
this would be time consuming and would
consume considerable storage for the copies
of the arrays.
51
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 12: f i g06_12. c
2 The name of an ar r ay i s t he same as &ar r ay[ 0 ] */
3 #i ncl ude <st di o. h>
4
5 / * f unct i on mai n begi ns pr ogr amexecut i on */
6 i nt mai n( voi d )
7 {
8 char ar r ay[ 5 ] ; / * def i ne an ar r ay of si ze 5 */
9
10 pr i nt f ( " ar r ay = %p\ n&ar r ay[ 0] = %p\ n &ar r ay = %p\ n",
11 ar r ay, &ar r ay[ 0 ] , &ar r ay ) ;
12
13 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
14
15 } / * end mai n */

ar r ay = 0012FF78
&ar r ay[ 0] = 0012FF78
&ar r ay = 0012FF78


Outline
f i g06_12. c
52
2007 Pearson Education, Inc. All rights reserved.
Software Engineering Observation 6.2
It is possible to pass an array by value (by
using a simple trick we explain in Chapter 10).
53
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 13: f i g06_13. c
2 Passi ng ar r ays and i ndi vi dual ar r ay el ement s t o f unct i ons */
3 #i ncl ude <st di o. h>
4 #def i ne SI ZE 5
5
6 / * f unct i on pr ot ot ypes */
7 voi d modi f yAr r ay( i nt b[ ] , i nt si ze ) ;
8 voi d modi f yEl ement ( i nt e ) ;
9
10 / * f unct i on mai n begi ns pr ogr amexecut i on */
11 i nt mai n( voi d )
12 {
13 i nt a[ SI ZE ] = { 0, 1, 2, 3, 4 }; / * i ni t i al i ze a */
14 i nt i ; / * count er */
15
16 pr i nt f ( " Ef f ect s of passi ng ent i r e ar r ay by r ef er ence: \ n\ nThe "
17 " val ues of t he or i gi nal ar r ay ar e: \ n" ) ;
18
19 / * out put or i gi nal ar r ay */
20 f or ( i = 0; i < SI ZE; i ++ ) {
21 pr i nt f ( " %3d" , a[ i ] ) ;
22 } / * end f or */
23
24 pr i nt f ( " \ n" ) ;
25
26 / * pass ar r ay a t o modi f yAr r ay by r ef er ence */
27 modi f yAr r ay( a, SI ZE ) ;
28
29 pr i nt f ( " The val ues of t he modi f i ed ar r ay ar e: \ n" ) ;
30

Outline
f i g06_13. c
(1 of 3 )
Function prototype indicates
function will take an array
Array a is passed to modifyArray
by passing only its name
54
2007 Pearson Education,
Inc. All rights reserved.
31 / * out put modi f i ed ar r ay */
32 f or ( i = 0; i < SI ZE; i ++ ) {
33 pr i nt f ( " %3d" , a[ i ] ) ;
34 } / * end f or */
35
36 / * out put val ue of a[ 3 ] */
37 pr i nt f ( " \ n\ n\ nEf f ect s of passi ng ar r ay el ement "
38 " by val ue: \ n\ nThe val ue of a[ 3] i s %d\ n", a[ 3 ] ) ;
39
40 modi f yEl ement ( a[ 3 ] ) ; / * pass ar r ay el ement a[ 3 ] by val ue */
41
42 / * out put val ue of a[ 3 ] */
43 pr i nt f ( " The val ue of a[ 3 ] i s %d\ n", a[ 3 ] ) ;
44
45 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
46
47 } / * end mai n */
48
49 / * i n f unct i on modi f yAr r ay, " b" poi nt s t o t he or i gi nal ar r ay " a"
50 i n memor y */
51 voi d modi f yAr r ay( i nt b[ ] , i nt si ze )
52 {
53 i nt j ; / * count er */
54
55 / * mul t i pl y each ar r ay el ement by 2 */
56 f or ( j = 0; j < si ze; j ++ ) {
57 b[ j ] *= 2;
58 } / * end f or */
59
60 } / * end f unct i on modi f yAr r ay */

Outline
f i g06_13. c
(2 of 3 )
Array element is passed to modifyElement
by passing a[ 3 ]
55
2007 Pearson Education,
Inc. All rights reserved.
61
62 / * i n f unct i on modi f yEl ement , " e" i s a l ocal copy of ar r ay el ement
63 a[ 3 ] passed f r ommai n */
64 voi d modi f yEl ement ( i nt e )
65 {
66 / * mul t i pl y par amet er by 2 */
67 pr i nt f ( " Val ue i n modi f yEl ement i s %d\ n", e *= 2 ) ;
68 } / * end f unct i on modi f yEl ement */

Ef f ect s of passi ng ent i r e ar r ay by r ef er ence:

The val ues of t he or i gi nal ar r ay ar e:
0 1 2 3 4
The val ues of t he modi f i ed ar r ay ar e:
0 2 4 6 8


Ef f ect s of passi ng ar r ay el ement by val ue:

The val ue of a[ 3] i s 6
Val ue i n modi f yEl ement i s 12
The val ue of a[ 3 ] i s 6


Outline
f i g06_13. c
(3 of 3 )
56
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 14: f i g06_14. c
2 Demonst r at i ng t he const t ype qual i f i er wi t h ar r ays */
3 #i ncl ude <st di o. h>
4
5 voi d t r yToModi f yAr r ay( const i nt b[ ] ) ; / * f unct i on pr ot ot ype */
6
7 / * f unct i on mai n begi ns pr ogr amexecut i on */
8 i nt mai n( voi d )
9 {
10 i nt a[ ] = { 10, 20, 30 }; / * i ni t i al i ze a */
11
12 t r yToModi f yAr r ay( a ) ;
13
14 pr i nt f ( " %d %d %d\ n", a[ 0 ] , a[ 1 ] , a[ 2 ] ) ;
15
16 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
17
18 } / * end mai n */
19
20 / * i n f unct i on t r yToModi f yAr r ay, ar r ay b i s const , so i t cannot be
21 used t o modi f y t he or i gi nal ar r ay a i n mai n. */
22 voi d t r yToModi f yAr r ay( const i nt b[ ] )
23 {
24 b[ 0 ] / = 2; / * er r or */
25 b[ 1 ] / = 2; / * er r or */
26 b[ 2 ] / = 2; / * er r or */
27 } / * end f unct i on t r yToModi f yAr r ay */

Outline
f i g06_14. c
(1 of 2 )
const qualifier tells compiler that
array cannot be changed
Any attempts to modify the array will
result in errors
57
2007 Pearson Education,
Inc. All rights reserved.

Compi l i ng. . .
FI G06_14. C
f i g06_14. c( 24) : er r or C2166: l - val ue speci f i es const obj ect
f i g06_14. c( 25) : er r or C2166: l - val ue speci f i es const obj ect
f i g06_14. c( 26) : er r or C2166: l - val ue speci f i es const obj ect


Outline
f i g06_14. c
(2 of 2 )
58
2007 Pearson Education, Inc. All rights reserved.
Software Engineering Observation 6.3
The const type qualifier can be applied to an
array parameter in a function definition to
prevent the original array from being modified
in the function body. This is another example
of the principle of least privilege. Functions
should not be given the capability to modify an
array unless it is absolutely necessary.
59
2007 Pearson Education, Inc. All rights reserved.
6.6 Sorting Arrays
Sorting data
Important computing application
Virtually every organization must sort some data
Bubble sort (sinking sort)
Several passes through the array
Successive pairs of elements are compared
- If increasing order (or identical ), no change
- If decreasing order, elements exchanged
Repeat
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
60
2007 Pearson Education, Inc. All rights reserved.
Performance Tip 6.4
Often, the simplest algorithms perform
poorly. Their virtue is that they are easy to
write, test and debug. However, more
complex algorithms are often needed to
realize maximum performance.
61
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 15: f i g06_15. c
2 Thi s pr ogr amsor t s an ar r ay' s val ues i nt o ascendi ng or der */
3 #i ncl ude <st di o. h>
4 #def i ne SI ZE 10
5
6 / * f unct i on mai n begi ns pr ogr amexecut i on */
7 i nt mai n( voi d )
8 {
9 / * i ni t i al i ze a */
10 i nt a[ SI ZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
11 i nt pass; / * passes count er */
12 i nt i ; / * compar i sons count er */
13 i nt hol d; / * t empor ar y l ocat i on used t o swap ar r ay el ement s */
14
15 pr i nt f ( " Dat a i t ems i n or i gi nal or der \ n" ) ;
16
17 / * out put or i gi nal ar r ay */
18 f or ( i = 0; i < SI ZE; i ++ ) {
19 pr i nt f ( " %4d" , a[ i ] ) ;
20 } / * end f or */
21
22 / * bubbl e sor t */
23 / * l oop t o cont r ol number of passes */
24 f or ( pass = 1; pass < SI ZE; pass++ ) {
25
26 / * l oop t o cont r ol number of compar i sons per pass */
27 f or ( i = 0; i < SI ZE - 1; i ++ ) {
28

Outline
f i g06_15. c
(1 of 2 )
62
2007 Pearson Education,
Inc. All rights reserved.
29 / * compar e adj acent el ement s and swap t hemi f f i r st
30 el ement i s gr eat er t han second el ement */
31 i f ( a[ i ] > a[ i + 1 ] ) {
32 hol d = a[ i ] ;
33 a[ i ] = a[ i + 1 ] ;
34 a[ i + 1 ] = hol d;
35 } / * end i f */
36
37 } / * end i nner f or */
38
39 } / * end out er f or */
40
41 pr i nt f ( " \ nDat a i t ems i n ascendi ng or der \ n" ) ;
42
43 / * out put sor t ed ar r ay */
44 f or ( i = 0; i < SI ZE; i ++ ) {
45 pr i nt f ( " %4d" , a[ i ] ) ;
46 } / * end f or */
47
48 pr i nt f ( " \ n" ) ;
49
50 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
51 }

Dat a i t ems i n or i gi nal or der
2 6 4 8 10 12 89 68 45 37
Dat a i t ems i n ascendi ng or der
2 4 6 8 10 12 37 45 68 89


Outline
f i g06_15. c
(2 of 2 )
If any two array elements are out of
order, the function swaps them
63
2007 Pearson Education, Inc. All rights reserved.
6.7 Case Study: Computing Mean,
Median and Mode Using Arrays
Mean average
Median number in middle of sorted list
1, 2, 3, 4, 5
3 is the median
Mode number that occurs most often
1, 1, 1, 2, 3, 3, 4, 5
1 is the mode
64
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 16: f i g06_16. c
2 Thi s pr ogr ami nt r oduces t he t opi c of sur vey dat a anal ysi s.
3 I t comput es t he mean, medi an and mode of t he dat a */
4 #i ncl ude <st di o. h>
5 #def i ne SI ZE 99
6
7 / * f unct i on pr ot ot ypes */
8 voi d mean( const i nt answer [ ] ) ;
9 voi d medi an( i nt answer [ ] ) ;
10 voi d mode( i nt f r eq[ ] , const i nt answer [ ] ) ;
11 voi d bubbl eSor t ( i nt a[ ] ) ;
12 voi d pr i nt Ar r ay( const i nt a[ ] ) ;
13
14 / * f unct i on mai n begi ns pr ogr amexecut i on */
15 i nt mai n( voi d )
16 {
17 i nt f r equency[ 10 ] = { 0 }; / * i ni t i al i ze ar r ay f r equency */
18
19 / * i ni t i al i ze ar r ay r esponse */
20 i nt r esponse[ SI ZE ] =
21 { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
22 7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
23 6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
24 7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
25 6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
26 7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
27 5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
28 7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
29 7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
30 4, 5, 6, 1, 6, 5, 7, 8, 7 };

Outline
f i g06_16. c
(1 of 6 )
65
2007 Pearson Education,
Inc. All rights reserved.
31
32 / * pr ocess r esponses */
33 mean( r esponse ) ;
34 medi an( r esponse ) ;
35 mode( f r equency, r esponse ) ;
36
37 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
38
39 } / * end mai n */
40
41 / * cal cul at e aver age of al l r esponse val ues */
42 voi d mean( const i nt answer [ ] )
43 {
44 i nt j ; / * count er f or t ot al i ng ar r ay el ement s */
45 i nt t ot al = 0; / * var i abl e t o hol d sumof ar r ay el ement s */
46
47 pr i nt f ( " %s\ n%s\ n%s\ n", " ********" , " Mean", " ********" ) ;
48
49 / * t ot al r esponse val ues */
50 f or ( j = 0; j < SI ZE; j ++ ) {
51 t ot al += answer [ j ] ;
52 } / * end f or */
53
54 pr i nt f ( " The mean i s t he aver age val ue of t he dat a\ n"
55 " i t ems. The mean i s equal t o t he t ot al of \ n"
56 " al l t he dat a i t ems di vi ded by t he number \ n"
57 " of dat a i t ems ( %d ) . The mean val ue f or \ n"
58 " t hi s r un i s: %d / %d = %. 4f \ n\ n",
59 SI ZE, t ot al , SI ZE, ( doubl e ) t ot al / SI ZE ) ;
60 } / * end f unct i on mean */

Outline
f i g06_16. c
(2 of 6 )
66
2007 Pearson Education,
Inc. All rights reserved.
61
62 / * sor t ar r ay and det er mi ne medi an el ement ' s val ue */
63 voi d medi an( i nt answer [ ] )
64 {
65 pr i nt f ( " \ n%s\ n%s\ n%s\ n%s",
66 " ********", " Medi an", " ********" ,
67 " The unsor t ed ar r ay of r esponses i s" ) ;
68
69 pr i nt Ar r ay( answer ) ; / * out put unsor t ed ar r ay */
70
71 bubbl eSor t ( answer ) ; / * sor t ar r ay */
72
73 pr i nt f ( " \ n\ nThe sor t ed ar r ay i s" ) ;
74 pr i nt Ar r ay( answer ) ; / * out put sor t ed ar r ay */
75
76 / * di spl ay medi an el ement */
77 pr i nt f ( " \ n\ nThe medi an i s el ement %d of \ n"
78 " t he sor t ed %d el ement ar r ay. \ n"
79 " For t hi s r un t he medi an i s %d\ n\ n",
80 SI ZE / 2, SI ZE, answer [ SI ZE / 2 ] ) ;
81 } / * end f unct i on medi an */
82
83 / * det er mi ne most f r equent r esponse */
84 voi d mode( i nt f r eq[ ] , const i nt answer [ ] )
85 {
86 i nt r at i ng; / * count er f or accessi ng el ement s 1- 9 of ar r ay f r eq */
87 i nt j ; / * count er f or summar i zi ng el ement s 0- 98 of ar r ay answer */
88 i nt h; / * count er f or di pl ayi ng hi st ogr ams of el ement s i n ar r ay f r eq */
89 i nt l ar gest = 0; / * r epr esent s l ar gest f r equency */
90 i nt modeVal ue = 0; / * r epr esent s most f r equent r esponse */

Outline
f i g06_16. c
(3 of 6 )
Once the array is sorted, the median will
be the value of the middle element
67
2007 Pearson Education,
Inc. All rights reserved.
91
92 pr i nt f ( " \ n%s\ n%s\ n%s\ n",
93 " ********", " Mode", " ********" ) ;
94
95 / * i ni t i al i ze f r equenci es t o 0 */
96 f or ( r at i ng = 1; r at i ng <= 9; r at i ng++ ) {
97 f r eq[ r at i ng ] = 0;
98 } / * end f or */
99
100 / * summar i ze f r equenci es */
101 f or ( j = 0; j < SI ZE; j ++ ) {
102 ++f r eq[ answer [ j ] ] ;
103 } / * end f or */
104
105 / * out put header s f or r esul t col umns */
106 pr i nt f ( " %s%11s%19s\ n\ n%54s\ n%54s\ n\ n",
107 " Response", " Fr equency", " Hi st ogr am",
108 " 1 1 2 2", " 5 0 5 0 5" ) ;
109
110 / * out put r esul t s */
111 f or ( r at i ng = 1; r at i ng <= 9; r at i ng++ ) {
112 pr i nt f ( " %8d%11d " , r at i ng, f r eq[ r at i ng ] ) ;
113
114 / * keep t r ack of mode val ue and l ar gest f r equency val ue */
115 i f ( f r eq[ r at i ng ] > l ar gest ) {
116 l ar gest = f r eq[ r at i ng ] ;
117 modeVal ue = r at i ng;
118 } / * end i f */

Outline
f i g06_16. c
(4 of 6 )
68
2007 Pearson Education,
Inc. All rights reserved.
119
120 / * out put hi st ogr ambar r epr esent i ng f r equency val ue */
121 f or ( h = 1; h <= f r eq[ r at i ng ] ; h++ ) {
122 pr i nt f ( " *" ) ;
123 } / * end i nner f or */
124
125 pr i nt f ( " \ n" ) ; / * bei ng new l i ne of out put */
126 } / * end out er f or */
127
128 / * di spl ay t he mode val ue */
129 pr i nt f ( " The mode i s t he most f r equent val ue. \ n"
130 " For t hi s r un t he mode i s %d whi ch occur r ed"
131 " %d t i mes. \ n", modeVal ue, l ar gest ) ;
132 } / * end f unct i on mode */
133
134 / * f unct i on t hat sor t s an ar r ay wi t h bubbl e sor t al gor i t hm*/
135 voi d bubbl eSor t ( i nt a[ ] )
136 {
137 i nt pass; / * pass count er */
138 i nt j ; / * compar i son count er */
139 i nt hol d; / * t empor ar y l ocat i on used t o swap el ement s */
140
141 / * l oop t o cont r ol number of passes */
142 f or ( pass = 1; pass < SI ZE; pass++ ) {
143
144 / * l oop t o cont r ol number of compar i sons per pass */
145 f or ( j = 0; j < SI ZE - 1; j ++ ) {
146

Outline
f i g06_16. c
(5 of 6 )
69
2007 Pearson Education,
Inc. All rights reserved.
147 / * swap el ement s i f out of or der */
148 i f ( a[ j ] > a[ j + 1 ] ) {
149 hol d = a[ j ] ;
150 a[ j ] = a[ j + 1 ] ;
151 a[ j + 1 ] = hol d;
152 } / * end i f */
153
154 } / * end i nner f or */
155
156 } / * end out er f or */
157
158 } / * end f unct i on bubbl eSor t */
159
160 / * out put ar r ay cont ent s ( 20 val ues per r ow) */
161 voi d pr i nt Ar r ay( const i nt a[ ] )
162 {
163 i nt j ; / * count er */
164
165 / * out put ar r ay cont ent s */
166 f or ( j = 0; j < SI ZE; j ++ ) {
167
168 i f ( j %20 == 0 ) { / * begi n new l i ne ever y 20 val ues */
169 pr i nt f ( " \ n" ) ;
170 } / * end i f */
171
172 pr i nt f ( " %2d" , a[ j ] ) ;
173 } / * end f or */
174
175 } / * end f unct i on pr i nt Ar r ay */

Outline
f i g06_16. c
(6 of 6 )
70
2007 Pearson Education,
Inc. All rights reserved.
********
Mean
********
The mean i s t he aver age val ue of t he dat a
i t ems. The mean i s equal t o t he t ot al of
al l t he dat a i t ems di vi ded by t he number
of dat a i t ems ( 99 ) . The mean val ue f or
t hi s r un i s: 681 / 99 = 6. 8788


********
Medi an
********
The unsor t ed ar r ay of r esponses i s
6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8
6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9
6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8
7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7

The sor t ed ar r ay i s
1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5
5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
(continued on next slide )

Outline
(1 of 2 )
71
2007 Pearson Education,
Inc. All rights reserved.
(continued from previous slide)
The medi an i s el ement 49 of
t he sor t ed 99 el ement ar r ay.
For t hi s r un t he medi an i s 7

********
Mode
********
Response Fr equency Hi st ogr am

1 1 2 2
5 0 5 0 5

1 1 *
2 3 ***
3 4 ****
4 5 *****
5 8 ********
6 9 *********
7 23 ***********************
8 27 ***************************
9 19 *******************
The mode i s t he most f r equent val ue.
For t hi s r un t he mode i s 8 whi ch occur r ed 27 t i mes.

Outline
(2 of 2 )
72
2007 Pearson Education, Inc. All rights reserved.
6.8 Searching Arrays
Search an array for a key value
Linear search
Simple
Compare each element of array with key value
Useful for small and unsorted arrays
73
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 18: f i g06_18. c
2 Li near sear ch of an ar r ay */
3 #i ncl ude <st di o. h>
4 #def i ne SI ZE 100
5
6 / * f unct i on pr ot ot ype */
7 i nt l i near Sear ch( const i nt ar r ay[ ] , i nt key, i nt si ze ) ;
8
9 / * f unct i on mai n begi ns pr ogr amexecut i on */
10 i nt mai n( voi d )
11 {
12 i nt a[ SI ZE ] ; / * cr eat e ar r ay a */
13 i nt x; / * count er f or i ni t i al i zi ng el ement s 0- 99 of ar r ay a */
14 i nt sear chKey; / * val ue t o l ocat e i n ar r ay a */
15 i nt el ement ; / * var i abl e t o hol d l ocat i on of sear chKey or - 1 */
16
17 / * cr eat e dat a */
18 f or ( x = 0; x < SI ZE; x++ ) {
19 a[ x ] = 2 * x;
20 } / * end f or */
21

Outline
f i g06_18. c
(1 of 3 )
74
2007 Pearson Education,
Inc. All rights reserved.
22 pr i nt f ( " Ent er i nt eger sear ch key: \ n" ) ;
23 scanf ( " %d" , &sear chKey ) ;
24
25 / * at t empt t o l ocat e sear chKey i n ar r ay a */
26 el ement = l i near Sear ch( a, sear chKey, SI ZE ) ;
27
28 / * di spl ay r esul t s */
29 i f ( el ement ! = - 1 ) {
30 pr i nt f ( " Found val ue i n el ement %d\ n", el ement ) ;
31 } / * end i f */
32 el se {
33 pr i nt f ( " Val ue not f ound\ n" ) ;
34 } / * end el se */
35
36 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
37
38 } / * end mai n */
39
40 / * compar e key t o ever y el ement of ar r ay unt i l t he l ocat i on i s f ound
41 or unt i l t he end of ar r ay i s r eached; r et ur n subscr i pt of el ement
42 i f key or - 1 i f key i s not f ound */
43 i nt l i near Sear ch( const i nt ar r ay[ ] , i nt key, i nt si ze )
44 {
45 i nt n; / * count er */
46

Outline
f i g06_18. c
(2 of 3 )
75
2007 Pearson Education,
Inc. All rights reserved.
47 / * l oop t hr ough ar r ay */
48 f or ( n = 0; n < si ze; ++n ) {
49
50 i f ( ar r ay[ n ] == key ) {
51 r et ur n n; / * r et ur n l ocat i on of key */
52 } / * end i f */
53
54 } / * end f or */
55
56 r et ur n - 1; / * key not f ound */
57
58 } / * end f unct i on l i near Sear ch */

Ent er i nt eger sear ch key:
36
Found val ue i n el ement 18



Ent er i nt eger sear ch key:
37
Val ue not f ound


Outline
f i g06_18. c
(3 of 3 )
Linear search algorithm searches
through every element in the
array until a match is found
76
2007 Pearson Education, Inc. All rights reserved.
6.8 Searching Arrays
Binary search
For sorted arrays only
Compares mi ddl e element with key
- If equal, match found
- If key < mi ddl e, looks in first half of array
- If key > mi ddl e, looks in last half
- Repeat
Very fast; at most n steps, where 2
n
> number of elements
- 30 element array takes at most 5 steps
2
5
> 30 so at most 5 steps
77
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 19: f i g06_19. c
2 Bi nar y sear ch of an ar r ay */
3 #i ncl ude <st di o. h>
4 #def i ne SI ZE 15
5
6 / * f unct i on pr ot ot ypes */
7 i nt bi nar ySear ch( const i nt b[ ] , i nt sear chKey, i nt l ow, i nt hi gh ) ;
8 voi d pr i nt Header ( voi d ) ;
9 voi d pr i nt Row( const i nt b[ ] , i nt l ow, i nt mi d, i nt hi gh ) ;
10
11 / * f unct i on mai n begi ns pr ogr amexecut i on */
12 i nt mai n( voi d )
13 {
14 i nt a[ SI ZE ] ; / * cr eat e ar r ay a */
15 i nt i ; / * count er f or i ni t i al i zi ng el ement s 0- 14 of ar r ay a */
16 i nt key; / * val ue t o l ocat e i n ar r ay a */
17 i nt r esul t ; / * var i abl e t o hol d l ocat i on of key or - 1 */
18
19 / * cr eat e dat a */
20 f or ( i = 0; i < SI ZE; i ++ ) {
21 a[ i ] = 2 * i ;
22 } / * end f or */
23
24 pr i nt f ( " Ent er a number bet ween 0 and 28: " ) ;
25 scanf ( " %d" , &key ) ;
26
27 pr i nt Header ( ) ;
28
29 / * sear ch f or key i n ar r ay a */
30 r esul t = bi nar ySear ch( a, key, 0, SI ZE - 1 ) ;

Outline
f i g06_19. c
(1 of 6 )
78
2007 Pearson Education,
Inc. All rights reserved.
31
32 / * di spl ay r esul t s */
33 i f ( r esul t ! = - 1 ) {
34 pr i nt f ( " \ n%d f ound i n ar r ay el ement %d\ n", key, r esul t ) ;
35 } / * end i f */
36 el se {
37 pr i nt f ( " \ n%d not f ound\ n", key ) ;
38 } / * end el se */
39
40 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
41
42 } / * end mai n */
43
44 / * f unct i on t o per f or mbi nar y sear ch of an ar r ay */
45 i nt bi nar ySear ch( const i nt b[ ] , i nt sear chKey, i nt l ow, i nt hi gh )
46 {
47 i nt mi ddl e; / * var i abl e t o hol d mi ddl e el ement of ar r ay */
48
49 / * l oop unt i l l ow subscr i pt i s gr eat er t han hi gh subscr i pt */
50 whi l e ( l ow <= hi gh ) {
51
52 / * det er mi ne mi ddl e el ement of subar r ay bei ng sear ched */
53 mi ddl e = ( l ow + hi gh ) / 2;
54
55 / * di spl ay subar r ay used i n t hi s l oop i t er at i on */
56 pr i nt Row( b, l ow, mi ddl e, hi gh ) ;
57

Outline
f i g06_19. c
(2 of 6 )
79
2007 Pearson Education,
Inc. All rights reserved.
58 / * i f sear chKey mat ched mi ddl e el ement , r et ur n mi ddl e */
59 i f ( sear chKey == b[ mi ddl e ] ) {
60 r et ur n mi ddl e;
61 } / * end i f */
62
63 / * i f sear chKey l ess t han mi ddl e el ement , set new hi gh */
64 el se i f ( sear chKey < b[ mi ddl e ] ) {
65 hi gh = mi ddl e - 1; / * sear ch l ow end of ar r ay */
66 } / * end el se i f */
67
68 / * i f sear chKey gr eat er t han mi ddl e el ement , set new l ow */
69 el se {
70 l ow = mi ddl e + 1; / * sear ch hi gh end of ar r ay */
71 } / * end el se */
72
73 } / * end whi l e */
74
75 r et ur n - 1; / * sear chKey not f ound */
76
77 } / * end f unct i on bi nar ySear ch */
78
79 / * Pr i nt a header f or t he out put */
80 voi d pr i nt Header ( voi d )
81 {
82 i nt i ; / * count er */
83
84 pr i nt f ( " \ nSubscr i pt s: \ n" ) ;
85

Outline
f i g06_19. c
(3 of 6 )
If value is found, return its index
If value is too high, search the left half of array
If value is too low, search the right half of array
80
2007 Pearson Education,
Inc. All rights reserved.
86 / * out put col umn head */
87 f or ( i = 0; i < SI ZE; i ++ ) {
88 pr i nt f ( " %3d ", i ) ;
89 } / * end f or */
90
91 pr i nt f ( " \ n" ) ; / * st ar t new l i ne of out put */
92
93 / * out put l i ne of - char act er s */
94 f or ( i = 1; i <= 4 * SI ZE; i ++ ) {
95 pr i nt f ( " - " ) ;
96 } / * end f or */
97
98 pr i nt f ( " \ n" ) ; / * st ar t new l i ne of out put */
99 } / * end f unct i on pr i nt Header */
100
101 / * Pr i nt one r ow of out put showi ng t he cur r ent
102 par t of t he ar r ay bei ng pr ocessed. */
103 voi d pr i nt Row( const i nt b[ ] , i nt l ow, i nt mi d, i nt hi gh )
104 {
105 i nt i ; / * count er f or i t er at i ng t hr ough ar r ay b */
106

Outline
f i g06_19. c
(4 of 6 )
81
2007 Pearson Education,
Inc. All rights reserved.
107 / * l oop t hr ough ent i r e ar r ay */
108 f or ( i = 0; i < SI ZE; i ++ ) {
109
110 / * di spl ay spaces i f out si de cur r ent subar r ay r ange */
111 i f ( i < l ow | | i > hi gh ) {
112 pr i nt f ( " " ) ;
113 } / * end i f */
114 el se i f ( i == mi d ) { / * di spl ay mi ddl e el ement */
115 pr i nt f ( " %3d*" , b[ i ] ) ; / * mar k mi ddl e val ue */
116 } / * end el se i f */
117 el se { / * di spl ay ot her el ement s i n subar r ay */
118 pr i nt f ( " %3d " , b[ i ] ) ;
119 } / * end el se */
120
121 } / * end f or */
122
123 pr i nt f ( " \ n" ) ; / * st ar t new l i ne of out put */
124 } / * end f unct i on pr i nt Row */

Ent er a number bet ween 0 and 28: 25

Subscr i pt s:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
16 18 20 22* 24 26 28
24 26* 28
24*

25 not f ound
(continued on next slide )

Outline
f i g06_19. c
(5 of 6 )
82
2007 Pearson Education,
Inc. All rights reserved.
(continued from previous slide)
Ent er a number bet ween 0 and 28: 8

Subscr i pt s:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
0 2 4 6* 8 10 12
8 10* 12
8*

8 f ound i n ar r ay el ement 4



Ent er a number bet ween 0 and 28: 6

Subscr i pt s:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
0 2 4 6* 8 10 12

6 f ound i n ar r ay el ement 3


Outline
f i g06_19. c
(6 of 6 )
83
2007 Pearson Education, Inc. All rights reserved.
6.9 Multiple-Subscripted Arrays
Multiple subscripted arrays
Tables with rows and columns (mby n array)
Like matrices: specify row, then column
Initialization
i nt b[ 2 ] [ 2 ] ={ { 1, 2 }, { 3, 4 } };
Initializers grouped by row in braces
If not enough, unspecified elements set to zero
i nt b[ 2 ] [ 2 ] ={ { 1 }, { 3, 4 } };
Referencing elements
Specify row, then column
pr i nt f ( " %d" , b[ 0 ] [ 1 ] ) ;
84
2007 Pearson Education, Inc. All rights reserved.
Common Programming Error 6.9
Referencing a double-subscripted array element
as a[ x, y ] instead of a[ x ] [ y ] . C interprets
a[ x, y ] as a[ y ] , and as such it does not cause
a syntax error.
85
2007 Pearson Education, Inc. All rights reserved.
Fig. 6.20 | Double-subscripted array with three rows and four columns.
86
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 21: f i g06_21. c
2 I ni t i al i zi ng mul t i di mensi onal ar r ays */
3 #i ncl ude <st di o. h>
4
5 voi d pr i nt Ar r ay( const i nt a[ ] [ 3 ] ) ; / * f unct i on pr ot ot ype */
6
7 / * f unct i on mai n begi ns pr ogr amexecut i on */
8 i nt mai n( voi d )
9 {
10 / * i ni t i al i ze ar r ay1, ar r ay2, ar r ay3 */
11 i nt ar r ay1[ 2 ] [ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
12 i nt ar r ay2[ 2 ] [ 3 ] = { 1, 2, 3, 4, 5 };
13 i nt ar r ay3[ 2 ] [ 3 ] = { { 1, 2 }, { 4 } };
14
15 pr i nt f ( " Val ues i n ar r ay1 by r ow ar e: \ n" ) ;
16 pr i nt Ar r ay( ar r ay1 ) ;
17
18 pr i nt f ( " Val ues i n ar r ay2 by r ow ar e: \ n" ) ;
19 pr i nt Ar r ay( ar r ay2 ) ;
20
21 pr i nt f ( " Val ues i n ar r ay3 by r ow ar e: \ n" ) ;
22 pr i nt Ar r ay( ar r ay3 ) ;
23
24 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
25
26 } / * end mai n */
27

Outline
f i g06_21. c
(1 of 2 )
array1 is initialized with both rows full
array2 and array3 are initialized only partially
87
2007 Pearson Education,
Inc. All rights reserved.
28 / * f unct i on t o out put ar r ay wi t h t wo r ows and t hr ee col umns */
29 voi d pr i nt Ar r ay( const i nt a[ ] [ 3 ] )
30 {
31 i nt i ; / * r ow count er */
32 i nt j ; / * col umn count er */
33
34 / * l oop t hr ough r ows */
35 f or ( i = 0; i <= 1; i ++ ) {
36
37 / * out put col umn val ues */
38 f or ( j = 0; j <= 2; j ++ ) {
39 pr i nt f ( " %d ", a[ i ] [ j ] ) ;
40 } / * end i nner f or */
41
42 pr i nt f ( " \ n" ) ; / * st ar t new l i ne of out put */
43 } / * end out er f or */
44
45 } / * end f unct i on pr i nt Ar r ay */

Val ues i n ar r ay1 by r ow ar e:
1 2 3
4 5 6
Val ues i n ar r ay2 by r ow ar e:
1 2 3
4 5 0
Val ues i n ar r ay3 by r ow ar e:
1 2 0
4 0 0


Outline
f i g06_21. c
(2 of 2 )
88
2007 Pearson Education,
Inc. All rights reserved.
1 / * Fi g. 6. 22: f i g06_22. c
2 Doubl e- subscr i pt ed ar r ay exampl e */
3 #i ncl ude <st di o. h>
4 #def i ne STUDENTS 3
5 #def i ne EXAMS 4
6
7 / * f unct i on pr ot ot ypes */
8 i nt mi ni mum( const i nt gr ades[ ] [ EXAMS ] , i nt pupi l s, i nt t est s ) ;
9 i nt maxi mum( const i nt gr ades[ ] [ EXAMS ] , i nt pupi l s, i nt t est s ) ;
10 doubl e aver age( const i nt set Of Gr ades[ ] , i nt t est s ) ;
11 voi d pr i nt Ar r ay( const i nt gr ades[ ] [ EXAMS ] , i nt pupi l s, i nt t est s ) ;
12
13 / * f unct i on mai n begi ns pr ogr amexecut i on */
14 i nt mai n( voi d )
15 {
16 i nt st udent ; / * st udent count er */
17
18 / * i ni t i al i ze st udent gr ades f or t hr ee st udent s ( r ows) */
19 const i nt st udent Gr ades[ STUDENTS ] [ EXAMS ] =
20 { { 77, 68, 86, 73 },
21 { 96, 87, 89, 78 },
22 { 70, 90, 86, 81 } };
23
24 / * out put ar r ay st udent Gr ades */
25 pr i nt f ( " The ar r ay i s: \ n" ) ;
26 pr i nt Ar r ay( st udent Gr ades, STUDENTS, EXAMS ) ;
27

Outline
f i g06_22. c
(1 of 6 )
Each row in the array corresponds to a
single students set of grades
89
2007 Pearson Education,
Inc. All rights reserved.
28 / * det er mi ne smal l est and l ar gest gr ade val ues */
29 pr i nt f ( " \ n\ nLowest gr ade: %d\ nHi ghest gr ade: %d\ n",
30 mi ni mum( st udent Gr ades, STUDENTS, EXAMS ) ,
31 maxi mum( st udent Gr ades, STUDENTS, EXAMS ) ) ;
32
33 / * cal cul at e aver age gr ade f or each st udent */
34 f or ( st udent = 0; st udent < STUDENTS; st udent ++ ) {
35 pr i nt f ( " The aver age gr ade f or st udent %d i s %. 2f \ n",
36 st udent , aver age( st udent Gr ades[ st udent ] , EXAMS ) ) ;
37 } / * end f or */
38
39 r et ur n 0; / * i ndi cat es successf ul t er mi nat i on */
40
41 } / * end mai n */
42

Outline
f i g06_22. c
(2 of 6 )
average function is passed a row of the array
90
2007 Pearson Education,
Inc. All rights reserved.
43 / * Fi nd t he mi ni mumgr ade */
44 i nt mi ni mum( const i nt gr ades[ ] [ EXAMS ] , i nt pupi l s, i nt t est s )
45 {
46 i nt i ; / * st udent count er */
47 i nt j ; / * examcount er */
48 i nt l owGr ade = 100; / * i ni t i al i ze t o hi ghest possi bl e gr ade */
49
50 / * l oop t hr ough r ows of gr ades */
51 f or ( i = 0; i < pupi l s; i ++ ) {
52
53 / * l oop t hr ough col umns of gr ades */
54 f or ( j = 0; j < t est s; j ++ ) {
55
56 i f ( gr ades[ i ] [ j ] < l owGr ade ) {
57 l owGr ade = gr ades[ i ] [ j ] ;
58 } / * end i f */
59
60 } / * end i nner f or */
61
62 } / * end out er f or */
63
64 r et ur n l owGr ade; / * r et ur n mi ni mumgr ade */
65
66 } / * end f unct i on mi ni mum*/
67

Outline
f i g06_22. c
(3 of 6 )
91
2007 Pearson Education,
Inc. All rights reserved.
68 / * Fi nd t he maxi mumgr ade */
69 i nt maxi mum( const i nt gr ades[ ] [ EXAMS ] , i nt pupi l s, i nt t est s )
70 {
71 i nt i ; / * st udent count er */
72 i nt j ; / * examcount er */
73 i nt hi ghGr ade = 0; / * i ni t i al i ze t o l owest possi bl e gr ade */
74
75 / * l oop t hr ough r ows of gr ades */
76 f or ( i = 0; i < pupi l s; i ++ ) {
77
78 / * l oop t hr ough col umns of gr ades */
79 f or ( j = 0; j < t est s; j ++ ) {
80
81 i f ( gr ades[ i ] [ j ] > hi ghGr ade ) {
82 hi ghGr ade = gr ades[ i ] [ j ] ;
83 } / * end i f */
84
85 } / * end i nner f or */
86
87 } / * end out er f or */
88
89 r et ur n hi ghGr ade; / * r et ur n maxi mumgr ade */
90
91 } / * end f unct i on maxi mum*/
92

Outline
f i g06_22. c
(4 of 6 )
92
2007 Pearson Education,
Inc. All rights reserved.
93 / * Det er mi ne t he aver age gr ade f or a par t i cul ar st udent */
94 doubl e aver age( const i nt set Of Gr ades[ ] , i nt t est s )
95 {
96 i nt i ; / * examcount er */
97 i nt t ot al = 0; / * sumof t est gr ades */
98
99 / * t ot al al l gr ades f or one st udent */
100 f or ( i = 0; i < t est s; i ++ ) {
101 t ot al += set Of Gr ades[ i ] ;
102 } / * end f or */
103
104 r et ur n ( doubl e ) t ot al / t est s; / * aver age */
105
106 } / * end f unct i on aver age */
107
108 / * Pr i nt t he ar r ay */
109 voi d pr i nt Ar r ay( const i nt gr ades[ ] [ EXAMS ] , i nt pupi l s, i nt t est s )
110 {
111 i nt i ; / * st udent count er */
112 i nt j ; / * examcount er */
113
114 / * out put col umn heads */
115 pr i nt f ( " [ 0] [ 1] [ 2] [ 3] " ) ;
116

Outline
f i g06_22. c
(5 of 6 )
93
2007 Pearson Education,
Inc. All rights reserved.
117 / * out put gr ades i n t abul ar f or mat */
118 f or ( i = 0; i < pupi l s; i ++ ) {
119
120 / * out put l abel f or r ow */
121 pr i nt f ( " \ nst udent Gr ades[ %d] ", i ) ;
122
123 / * out put gr ades f or one st udent */
124 f or ( j = 0; j < t est s; j ++ ) {
125 pr i nt f ( " %- 5d" , gr ades[ i ] [ j ] ) ;
126 } / * end i nner f or */
127
128 } / * end out er f or */
129
130 } / * end f unct i on pr i nt Ar r ay */

The ar r ay i s:
[ 0] [ 1] [ 2] [ 3]
st udent Gr ades[ 0] 77 68 86 73
st udent Gr ades[ 1] 96 87 89 78
st udent Gr ades[ 2] 70 90 86 81

Lowest gr ade: 68
Hi ghest gr ade: 96
The aver age gr ade f or st udent 0 i s 76. 00
The aver age gr ade f or st udent 1 i s 87. 50
The aver age gr ade f or st udent 2 i s 81. 75


Outline
f i g06_22. c
(6 of 6 )